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);
	}
	
	fclose($handle);
	fclose($f);
}

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

Leave a comment