After some testing with the code below I've come to this conclusion:
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;
}
}
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\"");
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.