PHP Code

How to download a file using php code

In this post, i want to explain download any file using php script. It will useful when we are developing a website like In that website have list of files and users can download those files. In my previous post i have explain how to get extension of file, using that post get the extension.

How to download a file using php code by Anil Kumar Panigrahi

How to download a file using php code by Anil Kumar Panigrahi

PHP Code

Script is as follows:

$filename = '[file name].[extension]';


// required for IE, otherwise Content-disposition is ignored
ini_set('zlib.output_compression', 'Off');

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");

// change, added quotes to allow spaces in filenames

header("Content-Disposition: attachment; filename="".basename($filename)."";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));


We can write this script for all types of files. We write this script in a function and just we pass the files with complete path. In this way we can use this script.

Hope it will be useful.


  • Reply
    Girish Girijan
    April 24, 2012 at 9:13 am



  • Reply
    Ananya Das
    May 18, 2012 at 11:40 am

    Thanks for the code.
    It works fine in Firefox and Google Chrome, but in IE. In IE, it downloads the php script itself.
    I have put your code inside a separate file (namely download.php) and set it as the action of my form. Inside the form, two input elements are there – one gets the filename and the other is a submit button that sends the filename to ‘$filename’ in download.php. This is required as random files are there to download.
    Please help. Thanks in advance.

  • Reply
    July 22, 2012 at 9:54 am

    its showing this error

    Warning: filesize() [function.filesize]: stat failed for c.doc in C:\xampp\htdocs\test1\download.php on line 22

    Warning: readfile(c.doc) [function.readfile]: failed to open stream: No such file or directory in C:\xampp\htdocs\test1\download.php on line 23

  • Reply
    July 22, 2012 at 10:07 am

    i got the error,extension was not properly defined.

    working properly in ie and chrome..

  • Reply
    October 25, 2012 at 4:28 am

    Thanks!!.. I’ll try this codes of yours and I’ll give you feedback 🙂


  • Reply
    November 10, 2012 at 7:04 am

    I m getting a error frequently,when i m using header function for download or redirect to another page.
    this error is getting in my application and also in my simple practise code….
    Can u help me plz…plz???
    in below i have written a simple program to redirect to another page through header….and my error is

    Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\practise14.php:1) in C:\xampp\htdocs\practise1.php on line 4

    also when i m using session code or cookie ,in that time also getting same header error .

    • Reply
      March 1, 2013 at 3:26 am

      use ob_start(); in the beginning of page and ob_flush(); at the end of page

  • Reply
    Naija Gist
    February 14, 2013 at 10:49 pm

    I believe this is among the such a lot significant information for me. And i’m glad reading your article. However should observation on some common issues, The website taste is perfect, the articles is in reality nice : D. Good process, cheers

  • Reply
    Amit Pinjani
    July 4, 2013 at 6:49 pm

    WHen i am clicking in Download Link for download images.. it download the “download.php” file itself.. what to do please help

  • Reply
    September 11, 2013 at 7:49 am

    can u explain download video files

  • Reply
    Download videos from Youtube using PHP and API - Anil Labs
    November 19, 2013 at 4:23 pm

    […] friends, In the earlier we learned how to download a file using php code, this post is explains how to download videos from Youtube using by PHP code and API. This post by […]

  • Reply
    April 18, 2014 at 6:45 am

    Relly good!!! please continue like this…

  • Reply
    June 4, 2014 at 12:17 pm

    hi sir ,
    I have need for the download all images from coding sent me sir from php .

  • Reply
    July 19, 2017 at 11:57 am

    I wanted to download video file. Using the above script but file is not getting downloaded fully. if its 125mb then it downloads 120 mb. So does not play after downloading. What could be the problem??

    My server is linux.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.