I am using the following code to hide the real file path to prevent hotlinking. Whenever I try to download the file through newly created address, everything including the file size and the file name are ok, but, when I try to open the file, whether a PDF file or a ZIP file, it says that the file is damaged.
When I download the file directly from the server, it works and it doesn't have any problems.
$file = 'file.pdf';
$path = $_SERVER['DOCUMENT_ROOT'].'/files/';
$file_Path = $path . $file;
list($file_Basename, $file_Ext) = explode('.', $file);
switch($file_Ext)
{
case "pdf": $ctype = "application/pdf"; break;
case "exe": $ctype = "application/octet-stream"; break;
case "zip": $ctype = "application/zip"; break;
case "rar": $ctype = "application/rar"; break;
case "doc": $ctype = "application/vnd.ms-word"; break;
case "xls": $ctype = "application/vnd.ms-excel"; break;
case "ppt": $ctype = "application/vnd.ms-powerpoint"; break;
case "gif": $ctype = "image/gif"; break;
case "png": $ctype = "image/png"; break;
default: $ctype = "application/force-download";
}
header('Content-Type:'. $ctype);
header('Content-Length:' . filesize($file_Path));
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile($file_Path);
exit();
What am I doing wrong?!