我有一个问题,已经多次上升,但我似乎无法找到我的解决方案!我正在尝试将pdf文件发送到客户端而不在浏览器中打开,文件下载但是当我打开它并且从原始文件中丢失了相当多的字节时它已损坏.我已经尝试了几种这样的方法来下载文件,但我只会向您展示我使用过的最新版本并希望得到一些反馈.
我还在文本编辑器中打开了下载的PDF,并且我可以看到它顶部没有php错误!
我也知道readfile()更快,但出于测试目的,我迫切希望得到任何工作,所以我使用了while(!feof())方法!
$file = __DIR__ . '/reports/somepdf.pdf';
$basename = basename($file);
$length = sprintf("%u", filesize($file));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $basename . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
ob_clean();
set_time_limit(0);
readfile($file);
另外要注意的是文件大小的差异:
Original: 351,873 bytes
Downloaded: 329,163 bytes