2012-04-26 36 views
6

我无法从我的网上商店提供.dmg。我剥离下来的代码,只是下面的调试,但无论怎样,我得到一个零字节文件送达:如何通过PHP/readfile提供.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。有什么建议么? Google教授不是我的朋友。

+0

如果文件名中没有空格,它是否工作? – Thilo 2012-04-26 04:19:09

回答

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 
?> 
+0

好的建议,但这不是问题 - 我所服务的所有文件在文件名中都有空格,并且它始终有效。 – 2012-04-26 17:37:57

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。

+0

所以问题是文件太大? – Thilo 2012-04-27 05:00:20

相关问题