2013-08-29 16 views
0

我有一個網站,MP3的下載完成後,當我下載它通過PHP(捲曲)這首歌被下載,但就像專輯封面歌曲的元數據,藝術家姓名等是丟失。PHP曲mp3下載正在重置元數據

在服務器上文件有所有的數據,但下載時一切都丟失了。 繼承人我的代碼:

if (!empty($path) && $path != null) 
      { 
       $ch = curl_init($path); 
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
       curl_setopt($ch, CURLOPT_HEADER, true); 
       $data = curl_exec($ch); 
       curl_close($ch); 
       if ($data === false) 
       { 
        echo 'CURL Failed'; 
        exit; 
       } 

       if (preg_match('/Content-Length: (\d+)/', $data, $matches)) 
       { 
        $contentLength = (int) $matches[1]; 
       } 

       header("Pragma: public"); 
       header("Expires: 0"); 
       header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
       header("Cache-Control: private", false); // required for certain browsers 
       header('Content-Type: audio/mpeg'); 
       header("Content-Disposition: attachment; filename=\"" . urlencode($song->title) . ".mp3\";"); 
       header('Content-Transfer-Encoding: binary'); 
       header('Content-Length: ' . $contentLength); 
       ob_clean(); 
       flush(); 
       echo $data; 
       exit; 
      } 

回答

0

你迫使捲曲的HTTP響應頭添加到您的MP3文件,所以你會得到的東西是這樣:

HTTP/1.1 ... 
Content-type: audio/mpeg 
Content-length: 12345 

ID3v2....... 
mp3 data here 

由於ID3數據在你發送的數據的開頭是不正確的,音頻播放器找不到它,因爲它看到的只是HTTP標題。

如果你正在使用這些標題提取內容長度,那麼爲什麼還要這麼做呢?您可以在PHP中使用strlen()爲您計算。例如

$mp3data = file_get_contents('url to mp3 file'); 
$length = strlen($mp3data); 

header("Content-length: $length"); 
echo $mp3data; 
+0

是否有可能也重命名文件? –

+0

很酷,它的工作,我剛剛添加了'header(「Content-Disposition:attachment; filename = \」「。urlencode($ song-> title)。」.mp3 \「;」);'它工作,非常感謝伴侶 –