Question

After some testing with the code below I've come to this conclusion:

  • The script works fine for AVI files in both firefox and chrome (if I change the content type to video/avi of course)
  • The script works fine for MKV files in Chrome. Firefox throws an error message in my face though (see below)
  • I've downloaded a 20 mb big test file (test7.mkv) from http://matroska.org/downloads/test_w1.html to test with a smaller file. Strangely Firefox can download that file and it seems to work. However if I try it on a 6 gb big file I get the firefox error you see below
  • Edit: Downloaded a 700 mb file, that didnt play at all in SM player (exitcode 1) but played fine in VLC player. I'm currently looking for a way to find how and where the file might be damaged.

My obvious question is: what am I doing wrong? How to properly download a mkv file in firefox or better in any common browser for that matter. I cant find anything helpful on google but maybe I'm just looking in the wrong places.

Firefox Error when trying to download a mkv file:

Corrupted Content Error

The page you are trying to view cannot be shown because an error in the data transmission was detected.

The page you are trying to view cannot be shown because an error in the data transmission was detected.Please contact the website owners to inform them of this problem.

    function download($file)
    {
        $path = $_SERVER['DOCUMENT_ROOT']; //<-- added the relative part after that 
        $fullPath = $path.$file;
        set_time_limit(0);
        if ($fd = fopen ($fullPath, "r")) 
        {
        $fsize = filesize($fullPath);
        $path_parts = pathinfo($fullPath);
        $ext = strtolower($path_parts["extension"]);

        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Type: video/x-matroska");
        header("Content-Disposition: attachment; filename=\"$file\""); 
        header("Content-Transfer-Encoding: binary");
        header("Content-length: $fsize");

        $file = @fopen($fullPath,"rb");
        if ($file) 
        {
            while(!feof($file)) 
            {
                print(fread($file, 1024*8));
                flush();
                if (connection_status()!=0) 
                {
                    @fclose($file);
                    die();
                }
            }
            @fclose($file);
        }
        exit;
    }
}
Was it helpful?

Solution 2

Solution:

I should've checked if the variables I define are set to the expected values. Turns out filesize() returns a signed int with 32 bit.

For now I just took the function that's shown here: https://stackoverflow.com/a/5502328/1232791

An other possible solution would be to not define the content length header. With this solution the client wont know how long his download is going to take though.

OTHER TIPS

the absolute path to an mkv file on the server

Firefox may be choking on the absolute path in the filename header value.

Try specifying mere a file name instead:

 $filename = pathinfo($filePath, PATHINFO_BASENAME);
 header("Content-Disposition: attachment; filename=\"$filename\"");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top