2010-10-13 28 views
1

你好有 好吧所以這是交易 我有我的mp3文件在我的服務器上,每個人都在自己的文件夾。 該文件夾中是MP3與下面的腳本PHP文件:可下載的MP3文件從PHP頭不工作

<?php 
// We'll be outputting a PDF 
header('Content-type: audio/mp3'); 

// It will be called file.mp3 
header('Content-Disposition: attachment; filename="mysong.mp3"'); 
// The PDF source is in original.mp3 
readfile("mysong.mp3"); 
?> 

的問題是,當我點擊進入該PHP頁面的標題是假設讓這個它會自動下載的MP3文件但當它下載它下載一個300KB的文件,但當我去的MP3文件的實際鏈接,它在瀏覽器中完美播放,所以即時猜測PHP文件提供標題的東西是錯誤的。

+0

觀看混亂評論;) – 2010-10-13 18:54:21

+0

嘗試了PHP手冊中的例子嗎? http://php.net/manual/en/function.readfile.php – 2010-10-13 18:56:28

回答

5

嘗試從http://us.php.net/readfile

<?php 
$file = 'monkey.gif'; 

if (file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
    exit; 
} 
?> 
+0

刪除此行:header('Content-Length:'。filesize($ file)); – 2015-10-01 06:49:12