dongyuyi5680 2019-03-14 11:56
浏览 142

下载php文件时随机添加字节

I am trying to download a binary file from the server that uses PHP. Somehow it randomly adds one byte at the front of the file, when downloading it:

Result download (Hex editor image):

Hex editor image

Expected result download (Hex editor image):

Things i have tried:
1. Headers aproach 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. Header aproach try 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 response download

$filename = 'spss-export.sav';
return response()->download($filename);

Header aproaches produce files that only have the random byte at the begining, but laravel aproach produces the random byte and one missing byte at the end of file. Anyone knows what might be the problem?

  • 写回答

1条回答 默认 最新

  • dongwuchu0314 2019-03-14 12:55
    关注

    Found an answer myself: You have to add ob_end_clean() before the file output. The laravel framework was somehow adding an extra space for files.

    Reference if this doesnt work:
    https://drupal.stackexchange.com/questions/163628/extra-space-at-beginning-of-downloaded-image/163644#163644

    评论

报告相同问题?

悬赏问题

  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?