Google SEO | PHP | Mysql | Ajax | Jquery | Photoshop | Illustrator | Database | Web Design | Web Development | Sitemap |

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.

Top Λ

Comments (29)

  • Nagesh

    Thank you so much.
    Monday-Feb-2014 | 11:23:57 AM
  • sumit18

    nice.
    Wednesday-Feb-2014 | 09:27:02 PM
  • feilong

    It helps me thank you.
    Friday-Jan-2014 | 03:04:51 PM
  • ravi kumar

    Coooolllll... :)
    Saturday-Jan-2014 | 05:10:59 PM
  • Wasim Sakharkar

    Wow, Working as expected. Thank You so very much.
    Thursday-Dec-2013 | 10:29:04 AM
  • ijii

    Good one..
    Wednesday-Dec-2013 | 11:41:51 PM
  • anichandran

    Great tutorial.. this blog helps me a lot, thanks, i have used this example in one of my work. http://www.creativeengg.in/Download.php
    Tuesday-Oct-2013 | 02:50:54 PM
  • priyankapatel

    Gud info for php.
    Wednesday-Oct-2013 | 02:02:56 PM
  • manoj

    thank you sir....
    Tuesday-Sep-2013 | 05:41:16 PM
  • Kiash

    There is not resume support.
    Sunday-Sep-2013 | 01:07:58 PM
  • Drew

    How do you call the download function, for instance I want to do something like this Download here
    Friday-Sep-2013 | 11:32:07 PM
  • khalfan

    iam a beginner in php... please guide me how to implement download.php page..
    Saturday-Aug-2013 | 12:05:15 AM
  • Imran Khan

    Nice script. Thanks for your sharing. Its sort my problem within a minute :).
    Saturday-Aug-2013 | 04:54:08 PM
  • krishna

    i want php srict for all video files and also high level and low leval videos download like mp4 and 3gp
    Sunday-Aug-2013 | 08:55:31 AM
  • alashov

    Thanxxxx it's helped me...
    Friday-Aug-2013 | 05:11:30 AM
  • alashov

    Thanxxxx it's helped me...
    Friday-Aug-2013 | 05:11:29 AM
  • Vijay Kumar

    Nice script. Thanks!
    Monday-Jul-2013 | 10:08:27 PM
  • Vijay Kumar

    Nice script. Thanks!
    Monday-Jul-2013 | 10:08:27 PM
  • internally1

    Great tutorial ever...this works prety gud.
    Friday-Jun-2013 | 10:32:52 PM
  • emraan

    Thanks
    very nice... download is working like charm.
    Tuesday-Apr-2013 | 03:16:42 PM
  • Narendra Singh Rajput

    Your blog is very good for PHP code help
    Tuesday-Mar-2013 | 07:24:58 PM
  • Dennis

    Thank you so much sir :) again thank you.
    Saturday-Mar-2013 | 12:20:40 PM
  • marcopolo

    Thanks, so much,...
    Thursday-Mar-2013 | 07:34:51 AM
  • Oladele idowu Joseph

    This is the best tutorial on the internet, in my entire 4 years of programming life; i have never come across a simple and easy-to-understand stuff like this kind, guys thanks so much.
    Friday-Feb-2013 | 11:58:38 PM
  • bam

    i need a file upload working full code..please help me out of this..thanks!Godbless
    Friday-Feb-2013 | 10:37:47 PM
  • poonam aher

    Good , this download script is working fine.thanks
    Thursday-Jan-2013 | 04:06:19 PM
  • Nora Sadiasa

    Thank you so much for the script more power to you people!!! I'm currently developing a joomla website and it is a great help, Thank you!
    Friday-Jan-2013 | 09:01:43 AM
  • Richard Ndagijimana

    Guys, You are the best. I was having a bloody nose with this download script. Thanks for Sharing :)
    Wednesday-Jan-2013 | 08:39:16 PM
  • Phil Tyler

    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.
    Thursday-Nov-2011 | 10:21:23 PM

Post Comment

Name:
Email:
Divider