• Web 2.0 platform and Programming Language

    We are promoting Web 2.0 platform in our template design, xHTML in our scripting and PHP programming language in our server side programming. So try our services and be satisfied with the outcome.

  • HTML/xHTML Standard

    This site passed and uses the W3C XHTML 1.0 Transitional standard, CSS level 2.1 driven, Javascript and with a latest trend of Table-less xHTML scripting by not using <table> tag in making our script.

  • Website History

    This website was founded last October 18, 2007, fully bought the domain February 2008 and has been continually updating. We are located at heart of The City of Golden Friendship Cagayan de Oro the City.

PHP Script to Force Download a file using Header Function

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 6 Comments Jan 19, 11 at 09:47 am

6 Responses to PHP Script to Force Download a file using Header Function

  1. help paying rent says:

    ……

    Whats up! I just want to give an enormous thumbs up for the good information you could have here on this post. I might be coming again to yo…

  2. obtain an ipad 2 for free press release says:

    ……

    Absolutely composed written content , thankyou for entropy….

  3. декоративни огради says:

    Just read this ……

    I like this site because so much useful material on here : D….

  4. vbox7 says:

    The following is a message…

    Hello there and thanks for your info � I�ve certainly picked up something new from proper hereI did on the other hand experience some technical issues the use of this web site, asIexperienced to reload the site a lot of occasions previous toImay ju…

  5. Male Impotence says:

    Great article…

    Just what I was looking for, appreciate it for posting ….

  6. detski pesni says:

    Great article…

    I also think therefore , perfectly pent post! ….

Leave a Reply

Your email address will not be published. Required fields are marked *

*


− two = 0

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

My Blogs

  • Configuring CodeIgniter for website with HTTPS protocol view more...
    Posted: Jan 05, 12 at 11:27 am
  • Keyword “Cagayan de Oro PHP Programmer” iWebProvider.com is Number 1 at Google Search view more...
    Posted: Feb 15, 11 at 05:54 pm
  • PHP get root URL and root Directory path view more...
    Posted: Feb 15, 11 at 02:47 am
  • PHP Object Oriented Programming (OOP) using Model View Controller (MVC) approach view more...
    Posted: Feb 12, 11 at 01:56 pm
  • Launching of the new 2011 iWebProvider.com Template Design view more...
    Posted: Feb 03, 11 at 08:17 pm

Most Popular Blogs

  • PHP get root URL and root Directory path view more...
    Posted: Feb 15, 11 at 02:47 am
  • Search Engine Friendly URL to convert .php and replace with .html file extension using .htaccess view more...
    Posted: Jan 19, 11 at 09:44 am
  • PHP Class for sending Email and preventing Email Spam view more...
    Posted: Jan 19, 11 at 09:41 am
  • PHP Function to Limit String output or display view more...
    Posted: Jan 19, 11 at 09:39 am

Wise Words

"Hard work surpasses genius"

by: Mark

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

by: Mark

"The function of good software is to make the complex appear to be simple."

by: Grady Booch

Latest Comments

abseiled... The best post I ever found that captured all the arguments around heading syntax was he...

by: Server

ablution... Every convenience brings its own inconveniences along with it....

by: VPS

abscises... What about "a collection of oldies..but goldies" ?...

by: Server

Keep in Touch

If you would like to find out more about us, just email us at inquiry[AT]iwebprovider.com