Google+SitemapRSS
Loading..

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.

Download FileDemo

//set the time out
set_time_limit(0);
//path to the file	
$file_path='files/'.$_REQUEST['filename'];
//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 */
 $known_mime_types=array(
 	"pdf" => "application/pdf",
 	"txt" => "text/plain",
 	"html" => "text/html",
 	"htm" => "text/html",
	"exe" => "application/octet-stream",
	"zip" => "application/zip",
	"doc" => "application/msword",
	"xls" => "application/vnd.ms-excel",
	"ppt" => "application/vnd.ms-powerpoint",
	"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.

Related Post

Phil Tyler
Thursday-Nov-2011 | 10:21:23 PM
The script seems great but when I try use it to download a 155Mb ZIP file it stalls and stops around the 130Mb-147Mb mark. The only error message is "download interrupted". The config of the server at FastHosts can be seen by visiting www.godfreyb.com/_scripts/test.php Can you suggest what might be wrong? If I directly create a hyperlink to the file in HTML and click it then it downloads perfectly and the ZIP is intact.
Name

Email

Comments

Enter Code
captcha image

auther
Blogger,Web Designer, Developer, Programmer
Subscribe via email to get updates

Category

The right Horizontal Machine or precision High Metal Removal Rate Machining
Copyrights © 2011 Webinfopedia. All rights reserved.