A Simple Server-to-Server File Transfer Script (PHP)

I’ve used this little script quite a bit while moving servers, and it’s saved me tons of time. It allows you to transfer large files from one server to another by breaking up the file into 1MB pieces to avoid memory limits. There seems to be a 2GB maximum on transfer sizes.

To use it, simply change the variable $path to a relative path on your new server where you want the file to be saved to, change the variable $url to the web-accessible location of the file on the old server, and upload and run the script on your new server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
set_time_limit(0); //Unlimited max execution time
 
$path = 'newfile.zip';
$url = 'http://example.com/oldfile.zip';
$newfname = $path;
echo 'Starting Download!<br>';
$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!<br>';
		}
}
if($file) {
	fclose($file);
}
if($newf) {
	fclose($newf);
}
echo 'Finished!';
?>

39 thoughts on “A Simple Server-to-Server File Transfer Script (PHP)

  1. Very nice,Simple and Lite script..

    It saved my lot of time . It transferred ~700 MB data with in Less than one minute

  2. I am using this for 3.5 gb file but this not work for me. what can i do for transferring 3.5 gig website to another hosting. the first hosting hasn’t cpanel.

  3. Great script!

    How exactly are the 1024 and 8 determined? For a file under 1MB, it will write about 20 lines of “1 MB File Chunk Written!”

  4. If the file is less than 1MB then we can reduce the size of chunks. Will it effect to the transferring speed (time)? and on server’s busyness 😉
    I think you can reply because you have written this short and nice script 🙂

  5. Thank you for your script……..

    We can say that it is a time saving mechanisms…..my 50MB file transferred just in 15 seconds …

    Keep it up dude…..

    Thanks again……

  6. if the newfile uses no path, like the sample, so it means the downloaded file will be placed at the same location as the php file right? because unlike the other, i can’t make this script run on my server 🙁

  7. Thank you for this. Have used it many times in the past. Has saved me a lot of work and given me lots of spare time to idle while working. Much appreciated.

  8. ohhhh my god! Bro you save my life… many many thanks to you. Your script worth $$100 million $$. you saved my lot of time…really salute to you! 🙂

  9. Hello,

    I have used above script to transfer 800 MB of files but after 500MB of transfer we are getting error “Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.”

    Do you have any idea to transfer big files through above script

    Thanks in Advance.
    Gulshan

  10. that is great sir its for very easy and powerful… again thanks
    i need two more scripts any body help me
    1. how can i transfer more than 2 GB, very big file like 14GB size
    2. how to transfer torrent data in my hosting site server? i mean same as this php scrip can i put any torent in my c panel server?

  11. Very useful snippet but contains some inaccuracies:

    1024 * 8 = 8192 = 8 kB

    Whereas for 1 MB you should have used:

    1024 * 1024 = 1048576 = 1 MB

  12. i upload my folder in remote server in zip form now i am trying to upload my php script file which will extract my zip folder on that location.
    but when i upload my php file. this executed itself and error occur.

    please help me to fix this error

    how to upload my project and unzip that folder with php code

Leave a Reply

Your email address will not be published. Required fields are marked *