0

I need to hide direct path of my mp3 files. So rather than an audio tag like "

<audio controls>
<source src="sound.mp3" type="audio/mp3">
</audio>

I used below :

<?php
    $file = 'sound.mp3';
    $fp = @fopen($file, 'rb');
    $size   = filesize($file); // File size
    $length = $size;           // Content length
    $start  = 0;               // Start byte
    $end    = $size - 1;       // End byte
    header('Content-type: video/mp4');
    //header("Accept-Ranges: 0-$length");
    header("Accept-Ranges: bytes");
    if (isset($_SERVER['HTTP_RANGE'])) {
        $c_start = $start;
        $c_end   = $end;
        list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
        if (strpos($range, ',') !== false) {
            header('HTTP/1.1 416 Requested Range Not Satisfiable');
            header("Content-Range: bytes $start-$end/$size");
            exit;
        }
        if ($range == '-') {
            $c_start = $size - substr($range, 1);
        }else{
            $range  = explode('-', $range);
            $c_start = $range[0];
            $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
        }
        $c_end = ($c_end > $end) ? $end : $c_end;
        if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
            header('HTTP/1.1 416 Requested Range Not Satisfiable');
            header("Content-Range: bytes $start-$end/$size");
            exit;
        }
        $start  = $c_start;
        $end    = $c_end;
        $length = $end - $start + 1;
        fseek($fp, $start);
        header('HTTP/1.1 206 Partial Content');
    }
    header("Content-Range: bytes $start-$end/$size");
    header("Content-Length: ".$length);
    $buffer = 1024 * 8;
    while(!feof($fp) && ($p = ftell($fp)) <= $end) {
        if ($p + $buffer > $end) {
            $buffer = $end - $p + 1;
        }
        set_time_limit(0);
        echo fread($fp, $buffer);
        flush();
    }
    fclose($fp);
    exit();
    ?>

But method 1 (direct) clearly is faster in first loading and also seeking. For example if sample.mp3 be 50MB, PHP Stream has about 5 seconds more latency in my server.

My question is WHY? and is there any way to make this stream faster through coding?

Thank you

Kranchi
  • 73
  • 1
  • 9
  • Not directly answering your question, but if you need to hide the path of your mp3, take a look at this approach https://stackoverflow.com/a/11286300/3355243 – Linesofcode May 07 '23 at 15:35
  • Why do you need to hide the path of your MP3 files? Why not configure your web server, like Nginx, to do this instead? Why not redirect to the URLs of the MP3 files directly? – Brad May 07 '23 at 15:55
  • @Brad I need to check user permission before streaming. Already I tested x-sendfile Apache module and its working good. You mean Nginx has some feature like it that I can use in PHP ? – Kranchi May 08 '23 at 08:46
  • @Linesofcode Thank you but I'm looking for server side strategy – Kranchi May 08 '23 at 08:48
  • @Kranchi if you're looking for a server side strategy, maybe create a temporary location of the mp3 and only when the user presses "play". Delete immediately afterwards. – Linesofcode May 08 '23 at 15:11
  • @Kranchi Yeah, Nginx can do the `sendfile` method as well. If it's working fine with Apache, then that's fine. Another strategy if you were using something like S3 for storage (AWS S3, Digital Ocean Spaces, Minio, Backblaze B2, Wasabi, etc.) is to sign a URL and redirect the user to it. The URL can be signed to only work within a few minutes, and is effectively generated for that individual user (assuming it isn't shared with others). But, the actual resource can be then stored on S3. – Brad May 08 '23 at 19:16

0 Answers0