How to bulk transfer many files over FTP using PHP

Have you ever wanted to transfer/upload many files using FTP, and sat there for hours watching the process and hoping that your FTP connection doesn’t time out?? Well, I have. This especially happens when I am uploading images, or a CMS. Well, I learned a little trick from Bo on how to use zipped files and PHP to make the file transfer process less painful.

This is how you do it:

Step 1: Zip everything you want copied.
Step 2: Upload the Zip file.
Step 3: Create a PHP file in the same directory called unzip.php and put this code in it:

  <?php<br />
echo '</p>
<pre>';<br />
system('unzip');<br />

Step 4: Run the PHP file in a browser (you should see the unzip output).
Step 5: Remove the zip file and the unzip.php file from your server.
Step 6: Rejoice that you just saved yourself a hour of waiting!

Isn’t that nice?

6 thoughts on “How to bulk transfer many files over FTP using PHP”

  1. That sounds more like using PHP to unzip an archive and not using PHP to transfer files via FTP like your title says.

    Still, an interesting and unique solution. Thanks for the info.

  2. Mark,

    Thanks for your comment. I see what you mean about the title though, it may have been a tad ambitious. The files are transferred in a zipped form via FTP and the unzipped using PHP on the server, reducing the time required to upload individual files, so the use of PHP is indirect here.

    Thanks for your feedback.

  3. Good tip, I’ve had to employ something similar esp with TinyMCE and such.

    The problem I’ve run into is that the unzipped files aren’t considered to be owned by me, so I can’t move, delete, CHMOD, or rename them – kind of a problem.

    The solution I’ve found is to use PCL Zip (, which allows you to CHMOD at the time of unzip, solving the problem.

    Of course, your script is a lot smaller than PclZip’s 300K, so if it works – cheers!

  4. george,

    Thanks for your comment and for the link. I’ll check it out. I recently used the PHP code above to extract a CMS after uploading the zip file to my hosting account. I didn’t notice any ownership issues. But I’ll ask Bo over at the Web Development Forums about it.

    Another thing I generally do is to use the cPanel file manager extracting utility to unpack the archive, and that generally works well too.

    thanks again george.


  5. Looks like you need a primer on importing & exporting MySQL files. 🙂

    Appears you didn’t import/export with the write character set.

    Why your getting all the weird coding….

Leave a Reply