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

时间:2016-08-09 23:32:47

标签: php

我想直接向浏览器发送文件:

<?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);

在访问者输入链接后,它会在Firefox和Chrome中显示正确的文件大小。但是它在jDownloader中不起作用。似乎jDownloader正在等待整个脚本结束。 当我尝试来自同一个webserver-jDownloader的普通静态文件时,会立即显示文件大小。 所以我的剧本显然有问题。

0 个答案:

没有答案