php MP3文件下載功能的實現


方式一:生成文件,返回一個鏈接,window.href = 鏈接;

方式二:hearder輸出文件流。

先設置流的Content-Type和web服務器的mime類型。

mime類型參考

一個header文件流下載mp3文件的實例,

服務器端:
function
download(){ $full_path = 'pro/mp3/demo.mp3'; $file_size = filesize($full_path); header("Content-type:audio/mpeg"); header("Accept-Ranges:bytes"); header("Accept-Length:$file_size"); header("Content-Disposition:attachment;filename=demo.mp3"); readfile($full_path); exit(); }
客戶端:
function down_mp3($url, $data){
       // curl下載文件
        $ch = curl_init();
       
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);       
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $mp3 = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Errno'.curl_error($ch);
        }
        
        curl_close($ch);
        // 保存文件到指定路徑
        file_put_contents("f:/voice/test.mp3" , $mp3);

        unset($mp3);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM