douju9272 2016-08-09 23:32
浏览 43

如何正确发送文件到浏览器?

I want to send a file directly to browser:

<?php
@ini_set('error_reporting', 0);

@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 'Off');


@ob_implicit_flush();

$file = 'ORG.iso';

$quoted    = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$file_size = filesize($file);


header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted); 
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

header('Content-Length: ' . $file_size);

@ob_end_flush();

set_time_limit(0);

$f = fopen($file, 'r');
while (!feof($f))
{
    $buff = fread($f, 204800);
    echo $buff;
    sleep(1);
}
fclose($f);

It shows correct file size in Firefox and Chrome right after visitor enters the link. However it does not work in jDownloader. It seems like jDownloader is waiting for the whole script to end. When I try a normal, static file from the same webserver- jDownloader shows file size instantly. So obviously something is wrong with my script.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥15 Python3.5 相关代码写作
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动