试试下面的代码:
<?php
function downloadFile($file){
$file_name = $file;
$mime = 'application/force-download';
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: close');
readfile($file_name);
exit();
}
?>
并调用函数:
<?php
downloadFile("./files/image.jpg");
?>
前提是文件“image.jpg”是正确的地址