PHP File Download Script

In this article am going to explain you how to create PHP file downloader to download any files from web server to local machine. This application works mainly on the header of the PHP.

PHP script to make force download any files

This type of file download script I have used in most of my web application. When you want to download any file you need to send the file name to this application, rest of the thing PHP will handle.

Find the example file and demo below.

Live Preview Download File

//set the time out
//path to the file	
//Call the download function with file path,file name and file type
output_file($file_path, ''.$_GET['filename'].'', 'text/plain');

Now check the file for extensions and permission

//File size
 $size = filesize($file);
 $name = rawurldecode($name);
 /* Figure out the MIME type */
 	"pdf" => "application/pdf",
 	"txt" => "text/plain",
 	"html" => "text/html",
 	"htm" => "text/html",
	"exe" => "application/octet-stream",
	"zip" => "application/zip",
	"doc" => "application/msword",
	"xls" => "application/",
	"ppt" => "application/",
	"gif" => "image/gif",
	"png" => "image/png",
	"jpeg"=> "image/jpg",
	"jpg" =>  "image/jpg",
	"php" => "text/plain"

Download the example file above and you can see the entire code in that. You can use this script to download most common file using PHP. Hope that this will help you. This function takes a path to a file to output ($file), the filename that the browser will see ($name) and the MIME type of the file ($mime_type, optional). Hope that it will help you.

Leave a Reply

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

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=""> <s> <strike> <strong>