【问题标题】:random added byte when downloading php file下载php文件时随机添加字节
【发布时间】:2019-08-05 07:32:07
【问题描述】:

我正在尝试从使用 PHP 的服务器下载二进制文件。 下载时不知何故在文件的前面随机添加一个字节:

结果下载(十六进制编辑器图片):

预期结果下载(十六进制编辑器图像):

我尝试过的事情:
1. 标头接近 try 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);

Header 方法生成的文件在开头只有随机字节,但 laravel 方法在文件末尾生成随机字节和一个缺失字节。任何人都知道可能是什么问题?

【问题讨论】:

  • 我认为您和 laravel 方式之间的区别在于 laravel 还发送带有文件大小的 Content-Length-header,这就是为什么在添加额外字节时最后缺少一个字节从头到尾。

标签: php laravel file binary byte


【解决方案1】:

自己找到了答案: 您必须在文件输出之前添加 ob_end_clean()。 laravel 框架不知何故为文件增加了额外的空间。

如果这不起作用,请参考:
https://drupal.stackexchange.com/questions/163628/extra-space-at-beginning-of-downloaded-image/163644#163644

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多