Git Product home page Git Product logo

smartreadfile's People

Contributors

thepag avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

smartreadfile's Issues

End of file can be lost if the requested range exceeds the file length

I have found this problem in the wild on a 20i web host.

On the 20i CDN, it seems that for paths that end in "streamable" extensions (e.g. \.mp3$, \.wav$) which are handled by PHP (e.g. rewritten in .htaccess), HTTP requests will always be split up behind the scenes into ranges of whole megabyte-sized blocks (e.g. bytes=0-1048575, bytes=1048576-2097151).

The smartReadFile function uses these values as given for $begin and $end, and the Content-Length will be based on these.

But for the final block, if it's not a whole megabyte, then the fread loop will go past the end of the file, less data will be returned than expected from the Content-Length, and 20i will return nothing for this block.

The effective result is that the amount of data received will be rounded down to the nearest whole megabyte.

I think this can be fixed simply by ensuring the $end value is only updated if the new value is lower than the original ($size - 1).

I can submit a pull request for this if that would be helpful, but PHP is not my most fluent language.

Works without Range

The initial code works well on desktop (Chrome OSX). But not on Safari (iOS).

I've comment out all about Range and now it works on iPhone too.

I did that because I've inspected requests on Chrome (desktop) and it seems it doesn't even use Ranges. Response is just 200 OK instead of 206 using Chrome on OSX.

Anyway, here is my current working code for Safari/Chrome on iOS (not tested on Android) :

$begin = 0;
$end = $size - 1;

// if (isset($_SERVER['HTTP_RANGE'])) {
//     if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
//         $begin = intval($matches[1]);
//         if (!empty($matches[2])) {
//             $end = intval($matches[2]);
//         }
//     }
// }
// if (isset($_SERVER['HTTP_RANGE'])) {
//     header('HTTP/1.1 206 Partial Content');
// } else {
    header('HTTP/1.1 200 OK');
// }

header("Content-Type: audio/mpeg");
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Accept-Ranges: bytes');
header('Content-Length:' . (($end - $begin) + 1));
// if (isset($_SERVER['HTTP_RANGE'])) {
//     header("Content-Range: bytes $begin-$end/$size");
// }
header("Content-Disposition: inline; filename=file.mp3");
header("Content-Transfer-Encoding: binary");
header("Last-Modified: $time");

\Log::info("=======");
\Log::info("Media : " . $location);
\Log::info("Range: " . (isset($_SERVER['HTTP_RANGE']) ? "Accepted" : "Not accepted"));
\Log::info("=======");

$cur = $begin;
fseek($fm, $begin, 0);

while (!feof($fm) && $cur <= $end && (connection_status() == 0)) {
    print fread($fm, min(1024 * 16, ($end - $cur) + 1));
    $cur += 1024 * 16;
}

PS : The code above doesn't work on Safari Desktop (works on Chrome desktop)

Php Maximum Execution Time 30 Seconds issue

hi,
i have used this SmartRead File and i'm facing problem while playing with videojs library that the server stops execution! and error in playing the file!
however how can i handle this situation without increase the execution time limit!
pls help to use it in optimal way! to serve videos!

thanks in advance

Stream Headers for Android

Recording this information here, due to relevancy. The original post on jPlayer google group may have more information.

Dom Wyss Wrote:

Hi there,

Dave Rodriguez and me did spend a bit of time on this.
For jPlayer to work with browsers on Android newer than 2.3, the stream must have different headers.
From this and this stackoverflow answers, we came to this solution:

<?php
$path       = __DIR__ . 'tracks/track.mp3';
$fileSize   = filesize($path); 
$strContext = stream_context_create(
   array(
       'http'=>array(
       'method'=>'GET',
       'header'=>"Accept-language: en\r\n"
       )
   )
);

header('Accept-Ranges:    none');
header('Cache-Control:    no-cache');
header('Content-type: audio/mpeg');
header('Pragma:    no-cache');

$userAgent = $_SERVER['HTTP_USER_AGENT'];

if (is_int(strpos($userAgent, 'Android')) && !is_int(strpos($userAgent, 'Android 2')))
{ 
    header('Transfer-Encoding:    none');
    header('Connection:    close');
}
else
{
  header('Content-Length:    ' . $fileSize);
}

$fpOrigin = fopen($path, 'rb', false, $strContext);
while(!feof($fpOrigin)){
    $buffer = fread($fpOrigin, 4096);
    echo $buffer;
    flush();
}
fclose($fpOrigin);
exit;

Hope this helps somebody!

All the best
Dom

Ensure the response is not GZIPed

Had reports of this not allowing seeking on Android, but it turned out to be that their response was being GZIPed. This was causing a few browsers to behave a little odd, but Android really did not like the data like that. .. I imagine other solutions wouldn't either, such as the Flash as already known.

Consider adding this header to smartReadFile() to ensure the output is not GZIPed:

header("Content-Encoding: none");

That way no one else will fall foul to this problem, where their server is automatically GZIPing all the output from PHP files.

Add option to break MIME and force download

So that this can be used to initiate downloads of the file.

Something like:

if($_GET['download'] !== null) {
  $type = 'application/octet-stream'
} else {
  switch ($file[count($file)-1]) {
    case 'mp3':
    $type = 'audio/mpeg';
    break;
    // All the other types...
  }
}

The idea being that we can simply give the download url parameter and the MIME type will be broken to trigger the browser to download it instead of trying to play it.

Not working on iPhone

I discover that my iPhone doesn't understand this header:
//header('Pragma: no-cache');

Commented all works fine, but I don't know why.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.