dpt62283 2014-02-11 04:44
浏览 16

php下载不一致的行为

i have a little download script setup here and it works well SOMETIMES. I have narrowed down the problem to the response headers being sent only majority of the times and when they dont what happens is that it asks me to download the file giving no information about it except for its name on disk and that its being downloaded as as 'application/octet-stream'. here are the headers i am sending

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$file->logical_name.'"');
header('Content-Type: "'.$mime.'"');
header("Content-Length: ".filesize($this->file));
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
readfile($this->file);  

so to be clear about my issue the file information is only being sent sometimes and when they dont i get my issue, which is no information about the file being sent hence it being downloaded as an weired named(as on disk) 'application/octet-stream'

UPDATE

this dosnt happen with smaller files around less than 100kb

  • 写回答

1条回答 默认 最新

  • doublestar2014 2014-02-11 06:32
    关注

    flushing all the output before reading the file solves the problem. seems the key was ensure all those headers were actually sent before tyring to download the file (reading the file with readfile())

    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename="'.$file->logical_name.'"');
    header('Content-Type: "'.$mime.'"');
    header('Content-Length:'.filesize($this->file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Pragma: no-cache');
    flush();
    readfile($this->file);
    

    adding the flush() function solved the problem. Note i didnt start a buffer with ob_start so i didnt do ob_flush but in the case you did this that might do it

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100