原创函数,在多种场景下实测有效:
function downfile($downname,$fileurl){
$filename=$fileurl;$temp=explode(".",$filename);
file_exists($fileurl) or die('file does not exist');
ob_clean();
header('Content-Description:File Transfer');
header('Content-Type:application/octet-stream');
header("Content-Transfer-Encoding:binary");
header("Content-Disposition:attachment;filename={$downname}.".end($temp));
header('Expires:0');
header('Cache-Control:no-cache,must-revalidate');
header("Pragma:no-cache");
header('Content-Length:'.filesize($fileurl));
readfile($fileurl);
exit;
}
test.php
ob_start();
downfile("newname","G:\old.jpg");