Reputation: 694
The following code works in Safari but not Firefox or Chrome
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
ob_end_flush();
exit;
The webpage at XXXX might be temporarily down or it may have moved permanently to a new web address. Error code: ERR_INVALID_RESPONSE
$file variable links to a file outside of the public_html directory.
I can't seem to find a fix, any ideas?
Upvotes: 1
Views: 1234
Reputation: 1025
I've had issue with filesize() on chrome and firefox in the past. This was my fix.
<?php
Function real_filesize($file) {
$fmod = filesize($file);
if ($fmod < 0) $fmod += 2.0 * (PHP_INT_MAX + 1);
$i = 0;
$myfile = fopen($file, "r");
while (strlen(fread($myfile, 1)) === 1) {
fseek($myfile, PHP_INT_MAX, SEEK_CUR);
$i++;
}
fclose($myfile);
if ($i % 2 == 1) $i--;
return ((float)($i) * (PHP_INT_MAX + 1)) + $fmod;
}
$filename = "somefile.ext";
$filepath = "some/dir/path/to/file/".$filename;
$filesize = real_filesize($filepath);
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream');
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $filesize);
ob_clean();
flush();
readfile($filepath);
exit();
?>
Upvotes: 1