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

    评论

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?