我正在尝试强制下载文件,我得到的只是浏览器窗口中随机字符串的未知字符。代码包括在内:
$file_url = "docs/$c/$path";
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$title.$type");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
header("Content-Length: ".filesize($file_url));
readfile($file_url);
它已经持续了大约一个星期,我疯了。任何帮助表示赞赏。 感谢。
答案 0 :(得分:1)
我怀疑你的Content-Disposition
标题是问题所在。
请尝试使用此版本:
header("Content-Disposition: attachment; filename=\"$title.$type\"");
答案 1 :(得分:0)
我遇到了同样的错误。使用ob_clean()和flush()帮助我的情况。我使用下面的代码:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename='.$filename.'.pdf');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-type: application/force-download');
header( 'Content-transfer-encoding: binary' );
ob_clean();
flush();
readfile('Reports/'.$filename.'.pdf');
exit;