2012-04-26 13 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'); 

、pdf。助言がありますか?教授のGoogleは私の友人ではありません。

+0

ファイル名にスペースがない場合は機能しますか? – Thilo

答えて

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

良い提案ですが、これは問題ではありません。私が提供しているファイルはすべてファイル名にスペースがあり、いつも働いています。 –

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