Subscribe For Free

Sunday, March 11, 2018

How to force downloading a file using PHP.

Here we are providing a simple code of explaining how to make a user to download file forcefully.

Normally, you don't necessarily need to use any server side scripting language like PHP to download images, zip files, pdf documents, exe files, etc. If such kind of file is stored in a public accessible folder, you can just create a hyperlink pointing to that file, and whenever a user click on the link, browser will automatically downloads that file. 

<a href="demo.jpg">Download</a>

Clicking a link that points to a PDF or an Image file will not cause it to download to your hard drive directly. It will only open the file in your browser. Further you can save it to your hard drive. However, zip and exe files are downloaded automatically to the hard drive by default.

function force_download($file)  
       if ((isset($file))&&(file_exists($file))) {  
            header("Content-length: ".filesize($file));  
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="' . $file . '"'); 
       } else {  
            echo "No file selected";  

$file = 'demo.jpg'; 

HTTP header fields

The Content-Length entity-header field indicates the size of the entity-body,
in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD
method, the size of the entity-body that would have been sent had the request
been a GET. 

The text/html content type is an Internet Media Type as well as a MIME content type. Using HTML in MIME messages allows the full richness of Web pages to be available in e-mail. text/plain[RFC1521] The text/plain content type is the generic subtype for plain text.

In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.

List of HTTP header fields

Similarly, you can force download other files formats like word doc, pdf files, etc.


  1. We have update list of do-follow blog commenting sites for new visitors, we will update this list regular basis so share our list to your friends also.
    the bookmarking sites

  2. Nice article keep posting like this article

  3. very good article....I am using PHP nOW

  4. Download 4Liker APK for Androi devices such as Smartphone or Tablet to get Facebook and Instagram auto likes on the posts you shared and increase the likes on your posts. instagram 4liker

  5. Good job
    Great job

  6. Collection of modern office furniture in Melbourne. Adept Office supplies extensive range of office furniture in Melbourne. We also sell quality home computer furniture.

  7. Smart cards or HID access cards are plastic identity card exactly like the dimensions of the driver’s license or credit card. The depth of a HID entry card may fluctuate based. Hid Access Card Copying

  8. thank you, this is exactly what I was looking for! Very helpful <3
    Keep up the good work!

  9. I am also associated with Goliath Cranes Manufacturers, EOT Cranes Manufacturers, by Pioneer Cranes is a leading Overhead Crane Manufacturers in India, and love to enjoy the stuff on the same as its rarely found on internet. Thanks again for writing such a good post.

  10. I am also associated with E Rickshaw Dealers, E Rickshaw Dealers, by Erickshaw Dealers is a leading E Rickshaw Dealers in India, and love to enjoy the stuff on the same as its rarely found on internet. Thanks again for writing such a good post.


  11. Alternative Aptoide App Stores are what have been most common these days. Gone are the days we depended upon the official app stores for our requirements of apps and games. aptoide ios

  12. I would like to read again your next write-ups.
    It was so nice article.
    Thanks for Sharing.
    Long term airport parking at jfk


click it