Search code examples
phpvideofseek

how to read a video file placed at server in php - video file's name includes http wrapper


I am using the following code but there appears to be warning with fseek and returns -1 instead of 0.

$file = fopen("http://www.example.com/public_html/data/video/temp.mov", "r") or exit("unable to open file");
fseek($file, -128, SEEK_END);

The file gets opened definately but fseek doesn't work. Is there some other method to read video from server?

Following is the error message

Warning: filesize() [function.filesize]: stat failed for 

Warning: fseek() [function.fseek]: stream does not support seeking in 

Solution

  • It must be some platform dependant problem. Try this code:

    $filename = "www.example.com/public_html/data/video/temp.mov";
    $file = fopen ($filename, "r")
        or exit("unable to open file");
    fseek ($file, filesize ($filename) - 128);
    

    HTTP wrapper does not support seeking. If you want to seek in a remote file thru HTTP you need to get the size of the file. One possible way is to interpret a directory listing, or make a HEAD request. When you know the filesize you can use curl and Range like here.

    This is also a method to get remote file size.

    How to download a file with cURL (This example loads the data into a PHP variable, use only if you want to process the data, If you just want to save it into an external file use CURLOPT_FILE instead of CURLOPT_RETURNTRANSFER):

    $c = curl_init (); 
    curl_setopt ($c, CURLOPT_URL, "http://example.com/some_dir/some_file.ext"); 
    curl_setopt ($c, CURLOPT_RANGE, max (0, $filesize - 128) . '-' . max (0, $filesize - 1));
    curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
    $content = curl_exec ();
    echo ($content);