folder-626334_1280

 

If you have been a developer long enough, at some point you would have had to move a large quantity of files from one server to another.  Now the manual method involves downloading everything via FTP, and then watching days pass while you upload it again to the new server.

This however requires copious amounts of patience and the involves several time-outs and files that need to be re-queued for transfer.  In short – it is a pain.

With necessity being the mother of invention, here is a short PHP script to use for transferring large files between two servers.
Note that you need to .zip the file / files up so that it you have one file that will be copied before starting on this script on your new server.  Create the script on the new server, add the URL to the backup in the script and you’re good to go.


<?php
set_time_limit(0); //Unlimited max execution time
$path = 'newfile.zip';
$url = 'http://www.path-to-old-file.com/backup.zip';
$newfname = $path;
echo 'Starting Download!
';
$file = fopen ($url, "rb");
if($file) {
$newf = fopen ($newfname, "wb");
if($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
echo '1 MB File Chunk Written!
';
}
}
if($file) {
fclose($file);
}
if($newf) {
fclose($newf);
}
echo 'Finished!';
?>

Save it as ‘upload.php’ and run it in a browser. It will immediately start to copy the file from the old server to yours. Once you see ‘Finished’ it is done.