Force download file in php

0

You must have come across to a situation where it reads “Download File” link and upon selecting that link it downloads the file ( not opening the file in browser). If you are looking for a way to force download file in PHP try the code given below.

Coding logic behind force download file?

The code for this script is very straight forward and easy. Take a look below.

  • Provide the name of the file available for download.
  • Check if its a file or not. If its a file proceed else abort.
  • Prevent the client from caching the response.
  • Get the mime type of the file.
  • Set the filename, mime type and file size in the header.
  • Read the file.

mime_type() is a function to get the mime type of the document. It has been taken from this LINK.

 

Share.
 

About Author

Entrepreneur & Dreamer

I am a passionate Software Professional, love to learn and share my knowledge with others.

Software is the hardware of my life.

Leave A Reply

CommentLuv badge

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.