PHP Streaming MP3

后端 未结 7 730
情深已故
情深已故 2020-12-02 19:12

I have a very similar situation to the person who asked: Can I serve MP3 files with PHP? Basically I am trying to protect mp3 files from direct download, so users have to go

7条回答
  •  甜味超标
    2020-12-02 19:59

    Here's what did the trick.

    $dir = dirname($_SERVER['DOCUMENT_ROOT'])."/protected_content";
    $filename = $_GET['file'];
    $file = $dir."/".$filename;
    
    $extension = "mp3";
    $mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";
    
    if(file_exists($file)){
        header('Content-type: {$mime_type}');
        header('Content-length: ' . filesize($file));
        header('Content-Disposition: filename="' . $filename);
        header('X-Pad: avoid browser bug');
        header('Cache-Control: no-cache');
        readfile($file);
    }else{
        header("HTTP/1.0 404 Not Found");
    }
    

提交回复
热议问题