Hyperlinking a file directly makes the file to download depending on the browser. If the browser has extensions available for the file type, it directly opens the file. Example PDF files. If PHP file is hyperlinked then it executes in the server itself, browser does not ask to download. If image file is hyperlinked then it will directly display in the browser.
In this article, I am going to explain how to force download any file. That will give more control over the file like whether to download and change download file name.
Before making the file force download, we need to check if the file exists and need to know few details about the file, like file type, extension.
Please check the below code.
function downloadFile($file, $new_filename = 'file', $delete_file = 0) {
if (!is_file($file)) {
die("<b>404 File not found!</b>");
//return 0;
}
$file_details = getFileDetails($file);
if ($new_filename == 'file') {
$new_filename = $file_details['filename'];
}else {
$new_filename .= '.' . $file_details['extension'];
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: " . $file_details['len']);
header("Content-Type: " . $file_details['ctype']);
header("Content-Disposition: attachment; filename=\"" . $new_filename . "\"");
header("Content-Transfer-Encoding: binary");
flush();
@readfile($file) or die("File not found.");
ob_flush();
flush();
if ($delete_file == '1') {
unlink($file);
}
exit;
}
function getFileDetails($file) {
//First, see if the file exists
if (!is_file($file)) {
die("<b>404 File not found!</b>");
}
$len = filesize($file);
$filename = basename($file);
$file_extension = getFileExtension($filename);
switch ($file_extension) {
case "pdf": $ctype = "application/pdf";
break;
case "exe": $ctype = "application/octet-stream";
break;
case "zip": $ctype = "application/zip";
break;
case "doc": $ctype = "application/msword";
break;
case "docx": $ctype = "application/msword";
break;
case "xls": $ctype = "application/vnd.ms-excel";
break;
case "xlsx": $ctype = "application/vnd.ms-excel";
break;
case "ppt": $ctype = "application/vnd.ms-powerpoint";
break;
case "pptx": $ctype = "application/vnd.ms-powerpoint";
break;
case "gif": $ctype = "image/gif";
break;
case "png": $ctype = "image/png";
break;
case "jpeg":
case "jpg": $ctype = "image/jpg";
break;
case "mp3": $ctype = "audio/mpeg";
break;
case "wav": $ctype = "audio/x-wav";
break;
case "mpeg":
case "mpg":
case "mpe": $ctype = "video/mpeg";
break;
case "mov": $ctype = "video/quicktime";
break;
case "avi": $ctype = "video/x-msvideo";
break;
case "swf": $ctype = "application/x-shockwave-flash";
break;
case "txt": $ctype = "text/plain";
break;
case "gz":
case "tgz": $ctype = "application/x-gzip";
break;
case "rss":
case "xml": $ctype = "text/xml";
break;
case "js": $ctype = "text/javascript";
break;
case "css": $ctype = "text/css";
break;
case "htm":
case "html": $ctype = "text/html;";
break;
default: $ctype = "application/force-download";
}
$return_array['filename'] = $filename;
$return_array['extension'] = $file_extension;
$return_array['len'] = $len;
$return_array['ctype'] = $ctype;
return $return_array;
}
function getFileExtension($filename) {
$filename = strtolower($filename);
$exts = explode(".", $filename);
$n = count($exts) - 1;
$exts = $exts[$n];
return $exts;
}
Hope this helps.