如何通过PHP/readfile提供.dmg文件?

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');
Run Code Online (Sandbox Code Playgroud)

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

Cay*_*ver 5

找到了解决方案。罪魁祸首是 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);
Run Code Online (Sandbox Code Playgroud)

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


Law*_*one 1

文件名中不应包含空格(网络托管文件时不应使用空格)

尝试类似的操作或重命名您的文件,不带空格:

<?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
?>
Run Code Online (Sandbox Code Playgroud)