2013-07-26 37 views
0

我開始我下載了PHP腳本,這是非常簡單的,看起來像這樣:下載使用PHP腳本 - .rar程序.rar.html成爲在Mac

$dir = 'downloads/'; 
$type = 'application/x-rar-compressed, application/octet-stream, application/zip'; 

function makeDownload($file, $dir, $type) 
{ 
    header("Content-Type: $type"); 
    header("Content-Disposition: attachment; filename=\"$file\""); 
    readfile($dir.$file); 
} 

if(!empty($_GET['file']) && !preg_match('=/=', $_GET['file'])) { 
    if(file_exists ($dir.$_GET['file']))  { 
     makeDownload($_GET['file'], $dir, $type); 
    } 

} 

它適用於WIN7 + FF /歌劇罰款/ chrome/safari,但在MAC上它會嘗試下載file.rar.html或file.zip.html而不是file.rar/file.zip。

任何想法爲什麼?

預先感謝

回答

0

「應用程序/ x-RAR壓縮的,應用程序/八位字節流,應用程序/壓縮」 並不是一個有效的文件類型。您需要在腳本中添加邏輯來檢測文件的類型,然後提供特定的文件類型。示例(未測試):

<?php 
$dir = 'downloads/'; 

function makeDownload($file, $dir) 
{ 
    switch(strtolower(end(explode(".", $file)))) { 
     case "zip": $type = "application/zip"; break; 
     case "rar": $type = "application/x-rar-compressed"; break; 
     default: $type = "application/octet-stream"; 
    } 
    header("Content-Type: $type"); 
    header("Content-Disposition: attachment; filename=\"$file\""); 
    readfile($dir.$file); 
    exit; // you should exit here to prevent the file from becoming corrupted if anything else gets echo'd after this function was called. 
} 

if(!empty($_GET['file']) && !preg_match('=/=', $_GET['file'])) { 
    if(file_exists ($dir.$_GET['file']))  { 
     makeDownload($_GET['file'], $dir); 
    } 

} 
?> 
+0

哇,就是這樣。非常感謝你! – knzo