V1rtualset
我不确定这个问题属于哪个领域,但我最接近的赌注是php配置。这是问题,我有一个来自SumEffect的电子商务平台,名为Digishop,基于php。允许客户下载产品的下载功能导致错误,这意味着我的客户无法获得他们的产品。 SumEffect支持已经洗手问题,说问题是我的。如果没有劝我说我是否适合运行服务器,有人可以看看这段代码和错误信息,然后猜测我可能会尝试修复它的内容吗?
Server 2008 R2 IIS 7.5 PhP 5.3.6
这是守则
$path = 'C:/Documents/Virtualsetworks/files/VSPHD1080S164.zip';
$downloadFile = basename($path);
$filesize = filesize($path);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $downloadFile . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
if($filesize){
header('Content-Length: ' . $filesize);
}
header('Content-Encoding: chunked'); //This is passed so in case the default encoding of the server uses compression a progress bar will display.
$handle = fopen($path, 'rb');
$chunksize = 1024;
$length = 0;
ob_start();
while($line = fread($handle, $chunksize)){
echo $line;
ob_end_flush();
ob_flush();
flush();
ob_start();
}
fclose($handle);
ob_end_clean();
>
这是客户端的错误: “F:\ E \ temp \ VSPHD1080S164(13).zip.part无法保存,因为无法读取源文件。 请稍后重试,或与服务器管理员联系。“
我的客户也得到了这个,这不仅仅是我。
它通常会读取9到10兆的文件,然后停止,等待一段时间,然后抛出该消息。有时它会读取0个字节,或者只读取几个字节,但大多数情况下它会达到9.3MB。 PHP.ini memory_limit为1024Mb,最大执行时间为1200.该文件夹具有所有正确的安全权限。有问题的文件介于100-1000Mb之间。 zlib.output_compression =关闭 max_execution_time = 1200 max_input_time = 60 memory_limit = 1024M IIS 7.5 Windows 2008 R2
任何帮助将不胜感激
答案 0 :(得分:1)
为什么在“转储文件内容”部分重复启动/停止输出缓冲?这是一个可怕的cpu周期浪费。只是做:
// open the file first thing, so the die() can be seen, if something fails
$handle = fopen($file, 'rb') or die("Unable to open $file");
// THEN output the download headers
header(...);
header(...);
// simple while loop, no need for any buffering/flushing.
while(!feof($handle)) {
echo fread($handle, $chunksize);
}
同样,您的'内容编码'标头不正确。对于分块下载,您使用Transfer-encoding
。由于您要发送整个文件,因此无需进行分块编码 - 无论如何您都没有进行正确的分块。使用分块传输时,您必须为每个块发送内容长度。