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

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.

//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.

PHP File Download Script

| PHP | 0 Comments
About The Author