Cay*_*ver 6 php dmg readfile http-headers
我没有运气从我的网上商店服务.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.有什么建议?谷歌教授不是我的朋友.
找到了解决方案。罪魁祸首是 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。
文件名中不应包含空格(网络托管文件时不应使用空格)
尝试类似的操作或重命名您的文件,不带空格:
<?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
?>
| 归档时间: | 
 | 
| 查看次数: | 1419 次 | 
| 最近记录: |