2014-02-07 45 views
0

我在使用PHP下載mp3文件時遇到問題。我需要下載該文件,並對其進行重命名。下面是它包含以下代碼的download.php文件:重命名mp3文件並在PHP中下載文件

$file = $_GET['file']; 
$flname = explode("/",$file); 
$num = sizeof($flname); 
$filenme = $flname[$num-1]; 
$name_of_file = $filenme; 

header('Content-Description: File Transfer'); 
header('Content-type: application/mp3'); 
header('Content-Disposition: attachment; filename="'.basename($file).'"'); 
header('Content-Length: '.filesize($name_of_file)); 
header('Content-Transfer-Encoding: binary'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Pragma: public'); 

readfile_chunked($file); 

function readfile_chunked($file) 
{ 
    $chunksize = 1*(1024*1024); // how many bytes per chunk 
    $buffer = ''; 
    $handle = fopen($filename, 'rb'); 

    if ($handle === false) 
    { 
     return false; 
    } 

    while (!feof($handle)) 
    { 
     $buffer = fread($handle, $chunksize); 
     print $buffer; 
    } 

    return fclose($handle); 
} 

我得到在$file文件路徑:

$file = $_GET['file']; 

其中$file變爲:

$file = "localhost/project/mp3 file path"; 

然後我爆炸它只獲取mp3文件名(從而刪除路徑)。 我不知道問題是什麼,但即使文件大小爲1-2MB,它在Firefox的下載對話框中也總是顯示大約490個字節。有人能解釋我做錯了什麼嗎?

預先感謝您。

+0

嘗試更改Content-Type,請參閱示例http://stackoverflow.com/questions/12017694/content-type-for-mp3-download-response –

回答

0

在文本編輯器中打開下載的文件。它可能包含php錯誤,它會告訴你究竟發生了什麼。這可能是您嘗試下載的文件的路徑或權限的問題。

+0

不,它沒有任何PHP錯誤。路徑也是正確的。我可以通過URL訪問它。但問題是與下載相關的代碼。 – user1638279