php - 下载php文件时随机添加字节
问题描述
我正在尝试从使用 PHP 的服务器下载二进制文件。下载时,它会以某种方式在文件的前面随机添加一个字节:
结果下载(十六进制编辑器图像):
预期结果下载(十六进制编辑器图像):
我尝试过的事情:
1.标题接近尝试1
$filename = 'spss-export.sav';
header("Content-Disposition: attachment; filename=survey_2_SPSS_syntax_file.sav");
header("Content-type: application/download; charset=UTF-8");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
readfile($filename); // do the double-download-dance (dirty but worky)
exit;
2.标题方法尝试2
$filename = 'spss-export.sav';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($filename) . "\"");
readfile($filename); // do the double-download-dance (dirty but worky)
exit;
3. Laravel 响应下载
$filename = 'spss-export.sav';
return response()->download($filename);
标头方法生成的文件在开头只有随机字节,但 laravel 方法在文件末尾生成随机字节和一个丢失的字节。任何人都知道可能是什么问题?
解决方案
自己找到了答案:您必须在文件输出之前添加ob_end_clean() 。laravel 框架以某种方式为文件添加了额外的空间。
推荐阅读
- java - Async 方法的行为不是异步的
- php - 如何根据 laravel 复选框值更新数据库
- arduino - 可以用 Node-red 和 Arduino 草图一起控制 Arduino 吗?
- python - Python 如何为列表中的每个项目创建集合?
- windows - 设置 Windows 资源管理器的视图默认布局
- c# - .NET 异步/等待,是否收集了变量垃圾?
- ssl-certificate - 免费和付费 SSL 证书握手之间的技术区别是什么?
- mysql - mysql sum(column) 从动态表中仅获取一行的值
- mysql - cast 属性用于外键查询而不是原生类型
- javascript - react-native 应用程序中的 Amazon Lex 应查询 SQL 数据库并回答用户问题