PHP Script to Force Download a file using Header Function

Posted by admin

This PHP script that will make the browser to open a new window and download a file instead of opening it. This script is helpful if you dont want a file to be open directly when using a browser, like if you want a javascript or simple text file to be downloaded.

function file_ext( $pathInfo, $type ){
$res = pathinfo($pathInfo);
return $res[$type];
}

function mime_type($filename){

/* get the file extension of a file name */
$extension = file_ext( $filename, 'extension' );

switch(strtolower($extension)){
case "js" :
return "application/x-javascript";
break;
case "json" :
return "application/json";
break;
case "jpg" :
case "jpeg" :
case "jpe" :
return "image/jpg";
break;
case "png" :
case "gif" :
case "bmp" :
case "tiff" :
return "image/".strtolower($extension);
break;
case "css" :
return "text/css";
break;
case "xml" :
return "application/xml";
break;
case "doc" :
case "docx" :
return "application/msword";
break;
case "xls" :
case "xlt" :
case "xlm" :
case "xld" :
case "xla" :
case "xlc" :
case "xlw" :
case "xll" :
return "application/vnd.ms-excel";
break;
case "ppt" :
case "pps" :
return "application/vnd.ms-powerpoint";
break;
case "rtf" :
return "application/rtf";
break;
case "pdf" :
return "application/pdf";
break;
case "html" :
case "htm" :
case "php" :
return "text/html";
case "txt" :
return "text/plain";
break;
case "mpeg" :
case "mpg" :
case "mpe" :
return "video/mpeg";
break;
case "mp3" :
return "audio/mpeg3";
break;
case "wav" :
return "audio/wav";
break;
case "aiff" :
case "aif" :
return "audio/aiff";
break;
case "avi" :
return "video/msvideo";
break;
case "wmv" :
return "video/x-ms-wmv";
break;
case "mov" :
return "video/quicktime";
break;
case "zip" :
return "application/zip";
break;
case "tar" :
return "application/x-tar";
break;
case "swf" :
return "application/x-shockwave-flash";
break;
default :
return "application/octet-stream";
break;
}
}


/*
Get the value of the variable file_name GET
value of variable file_name will then be decode
from its URL format and also decode from its
base64 encoding.
*/
$file_name = base64_decode(urldecode($_GET['file_name']));

/* return the MIME Type of the File */
$content_type = mime_type($file_name);

/* Change to file the directory location of the file */
$file_root_loc = 'public_html/uploads/';

header('Content-type: '.$content_type);
header('Content-Disposition: attachment; filename="'.$file_name.'"');
readfile($file_root_loc.$file_name);
Back to List | Comments (0) | November 06, 2009 at 04:39 pm

0 Responses

No Records Yet.

Leave a Reply

Name (required)

Email (required) will not be publish

Website (optional) ex: http://www.domain.com

Notify me of followup comments via e-mail. (optional)

Your Message (required)

Enter Security Code (required)
captcha  

Latest News (2)

iWebProvider.com No 1 at Yahoo search engine for the keyword -filipino web developer.

Posted on October 02, 2009

On Sept 6, 2009 iWebProvider.com is No. 1 at Yahoo and Google for keyword "filipino web developer".

continue reading

Launching of the updated iWebProvider.com

Posted on October 02, 2009

Now as of July 7, 2009 the launching of iWebProvider.com with its improve feature and the used of its very own CMS. It now has features like Blogs, comments on News articles and the upcoming user registration.

continue reading

Wise Words

"It's not about how many you have learned but how much you have utilized and mastered what you have learned."

- by: Mark Lester
Get subscribers