subject: How to Force a File Download Using PHP [print this page] How to Force a File Download Using PHP How to Force a File Download Using PHP
A common usage and requirement of a lot of blogger, developers and programmers is a technique of "Force a File Download". As a PHP programmer, i will like to share a built-in mechanism of PHP which will do this job efficiently.
Before going to explain in detail, i would like to break-up some important parts of this logic as i do in my previous post "PHP random password generator"
1. File Name
It is self-explanatory - as we require a file name to download. Make sure it is on the server. Otherwise i can bet, Very very perfect PHP script cannot offer to download this file ;). It is easy, yes very easy to ensure the validity of file, that will be discussed in the later section.
$file = "logo.jpg";
Waoo, we are successful in the completion of our first step.
2. File Path
We will require a "Valid Absolute Path" to the file directory. Example can be, "E:/wamp/www/download/ " and by combining our File Name, it will be,
The path is an example of Windows environment. Now it is time to validate this path and file name. Again, it is easy by using the built-in methods of PHP.
If our script fails to find the exact path, execution will be halt, and will said "File not found or inaccessible!" at its last breath.
But i am sure, you are smart to fix it by giving the correct path. and i am happy as we have completed our second variable as well.
3. Content Type (Mimi Type) of a File
It is very important to know about the mimi-type of a file which we are going to offer to download, yes of course Force Download. Every file has a specific mimi-type which helps the browser to offer download in a correct way. Example of mimi-type can be,
pdf: application/pdf
txt: text/plain
exe: application/octet-stream
zip: application/zip
gif: image/gif
png: image/png
jpg: image/jpg
jpeg: image/jpeg
Don't worry, it is not important to learn every mimi-type. PHP has very smart solutions to get the mimi-type of a file automatically, that's why "I Love PHP". You can use any of below method to determine the mimi-type by using $file_path variable.
:( - You have not any of above function enabled on your PHP server. Friend, consider to change your hosting, No, really not - you still want to stick with that, Okay don't worry, we can try a standard mimi-type that will help in force downloading a file.
if( empty( $file_mime_type ) ) {
$file_mime_type = "application/force-download";
}
Great, our third step is completed. We have assigned a proper value to $file_mime_type.
4. File Size
I promise, it is last step - as we have to require to determine the file size before executing our final algorithm. Let's arrange some weighing scale to do this. Yes, PHP has a built-in weight scale to accomplish this task.
$file_size = filesize($file_path);
All in One
We have prepared our four inputs. and we are ready to write final algorithm.
$file = "logo.jpg";
$file_path = "E:/wamp/www/download/logo.jpg";
$file_mime_type = "image/jpg"; // Auto / Default Detection
$file_size = filesize($file_path);
Ready to offer Force Download - readfile()
I will use readfile(), built-in method of PHP, which is very powerful to this job. Following is tested algorithm which i am using in my "Free PHP Download File Script".