Anjanesh

Assignment Statements, Comparisons & Observations
Font: Size: + -

Uploading and Extracting files without Shell Access on a Shared Host

Sunday, September 28, 2008

The easiest way to upload a large number of files to your website hosting server is to zip the files first on your local PC, upload the zip file to your hosting account and then use a shell command like unzip to extract. Unfortunately, most shared hosting environments don't provide shell access due to security reasons. But from a business perspective, users who require shell access access would be really requiring a custom setup on a dedicated server.

But if you have an entire directory of files to upload to your shared hosting account, there is still a way to extract the contents online using a simple PHP script. For this to work you got to have PHP 5.2 or later enabled.

<?php
if (isset($_GET['f']))
 $file = $_GET['f'];
else
 die("arg 1 required");

$zip = new ZipArchive;

if ($zip->open($file) === FALSE)
 die("Failed to open $file");

if (!$zip->extractTo('.'))
 echo "Extract not successful";
else
 echo "Extract successful";

$zip->close();
?>
  1. Zip the folder on your local PC using WinZip or whatever (say sample.zip).
  2. Upload the zip file sample.zip via FTP to your host (say in temp folder).
  3. Upload the PHP file unzip.php to temp folder.
  4. Enter in your browser the following address http://mywebsite.com/temp/unzip.php?f=sample.zip
  5. If you look in your host server's temp folder, you should see the contents of sample.zip extracted.

DAOne issue you would face is the owner permissions of that extracted directory. Since the extract part was done by a script, the owner of that folder would be apache instead of your username. The only workaround of this is to login to your web control panel, browse files and reset ownership of that folder. For example, in a control panel like DirectAdmin you could just Browse for files and click on Recursively.

0 comments: