Streaming mp3 file through php

前端 未结 3 1452
太阳男子
太阳男子 2020-12-31 20:39

Here is my php code to stream mp3 file through php

set_time_limit(0);
$dirPath = \"path_of_the_directory\";
$songCode = $_REQUEST[\'c\'];
$filePath = $dirPat         


        
3条回答
  •  攒了一身酷
    2020-12-31 20:55

    Why content-type: application/octet-stream if it's a song? Change the headers:

    set_time_limit(0);
    $dirPath = "path_of_the_directory";
    $songCode = $_REQUEST['c'];
    $filePath = $dirPath . "/" . $songCode . ".mp3";
    $strContext=stream_context_create(
        array(
            'http'=>array(
            'method'=>'GET',
            'header'=>"Accept-language: en\r\n"
            )
        )
    );
    $fpOrigin=fopen($filePath, 'rb', false, $strContext);
    header('Content-Disposition: inline; filename="song.mp3"');
    header('Pragma: no-cache');
    header('Content-type: audio/mpeg');
    header('Content-Length: '.filesize($filePath));
    while(!feof($fpOrigin)){
      $buffer=fread($fpOrigin, 4096);
      echo $buffer;
      flush();
    }
    fclose($fpOrigin);
    

    LE: removed Content-Transfer-Encoding and changed Content-Disposition from attachment to inline

提交回复
热议问题