6

我没有运气从我的在线商店提供 .dmg。我将代码简化为以下代码以进行调试,但无论我得到一个零字节文件是什么:

header('Content-Type: application/x-apple-diskimage');   // also tried octet-stream
header('Content-Disposition: attachment; filename="My Cool Image.dmg"');
$size = filesize('/var/www/mypath/My Cool Image.dmg');
header('Content-Length: '.$size);
readfile('/var/www/mypath/My Cool Image.dmg');

相同的代码适用于我提供的许多其他文件类型:bin、zip、pdf。有什么建议么?谷歌教授不是我的朋友。

4

2 回答 2

4

找到了解决方案。罪魁祸首是 readfile() 并且可能与内存有关。代替 readfile() 行,我使用这个:

$fd = fopen ('/var/www/mypath/My Cool Image.dmg', "r");
while(!feof($fd)) {
    set_time_limit(30);
    echo fread($fd, 4096);
    flush();
}
fclose ($fd);

它现在可以正确处理所有文件类型,包括 DMG。

于 2012-04-26T18:22:38.320 回答
1

文件名中不应包含空格(对于 Web 托管文件,不应使用空格)

尝试这样的事情或重命名你的文件,没有空格:

<?php 
$path ='/var/www/mypath/';
$filename = 'My Cool Image.dmg';

$outfile = preg_replace('/[^a-zA-Z0-9.-]/s', '_', $filename);

header('Content-Type: application/x-apple-diskimage');   // also tried octet-stream
header('Content-Disposition: attachment; filename="'.$outfile.'"');

header('Content-Length: '.sprintf("%u", filesize($file)));

readfile($path.$filename); //This part is using the real name with spaces so it still may not work
?>
于 2012-04-26T04:37:29.713 回答