PHP – File Download

|

When WGET isn’t available PHP comes to the rescue. In this example $path file name can be different than the $url file name or the same, if it is different it will be saved with the file name in $path

set_time_limit(0); //Unlimited max execution time
 
$path = 'filetodownload.txt';
$url = 'http://domain.com/currentfilename.txt';
$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!';
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.