PHP download remote file via url

The following function will download and save a remote file , using its http url :

function download_remote($url , $save_path)
	$f = fopen( $save_path , 'w+');
	$handle = fopen($url , "rb");
	while (!feof($handle)) 
		$contents = fread($handle, 8192);
		fwrite($f , $contents);

The method reads 8192 bytes at a time , and not the whole file , since if the whole file is read at once , then it my exceed the total memory available to php.

Last Updated On : 19th October 2011

Subscribe to get updates delivered to your inbox

About Silver Moon

Php developer, blogger and Linux enthusiast. He can be reached at [email protected]. Or find him on

Leave a comment