php readfile error 500
Q:
To client download a file, i use script below:
ob_start();
$file='http://media.xxx.com/media/Audio/radio_so_69.mp3';
$name='radio_so_69.mp3';
header("Content-type: application/force-download");
header("Content-Disposition: attachment;filename=".$name);
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
readfile($file);
ob_flush();
But i receive "500 Internal Server Error"
How to slove this problem ?
A:
if (file_exists($file)) {
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);
exit;
}
or simple
$file='http://media.xxx.com/media/Audio/radio_so_69.mp3';
$name='radio_so_69.mp3';
if (file_exists($file)) {
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment;filename=".$name);
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
ob_clean();
flush();
readfile($file);
}