I am trying to create a force-download page to prevent browsers from opening files that should be downloaded. The problem is that the downloaded file has 0 bytes and thus is unusable. What is wrong with my code?
$file = "http://gh0stsec.zxq.net/background1.jpg";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
header("Content-Description: File Transfer");
@readfile($file);
exit();
check:
header("Content-Length: ".filesize($filename));
I think it should be:
header("Content-Length: ".filesize($file));
I think you're going to have an issue with filesize
since it's not a local file (if it is, use a relative path). Content-Type
headers are always a little weird for me, but in all of the examples I read, force-download
was always a fallback. Anyways, I did this and it seemed to work:
<?php
$file = file_get_contents('http://gh0stsec.zxq.net/background1.jpg');
if ($file)
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=background1.jpg');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($file));
ob_clean();
flush();
echo $file;
}
else
{
echo 'error';
}
?>