How to Force a File Download Using PHP
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,
$file_path = "E:/wamp/www/download/" . $file; /* E:/wamp/www/download/logo.jpg */
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( empty( $file ) || !file_exists( $file_path ) ) {
exit("File not found or inaccessible!");
}
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.
a. By mimi_content_type
if( function_exists( 'mime_content_type' ) ) {
$file_mime_type = @mime_content_type( $file_path );
}
b. By finfo_file
if( function_exists( 'finfo_file' ) ) {
$finfo = @finfo_open(FILEINFO_MIME);
$file_mime_type = @finfo_file($finfo, $file_path);
finfo_close($finfo);
}
:( - 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".
header('Content-Description: File Transfer');
header('Content-Type: ' . $file_mime_type);
header('Content-Disposition: attachment; filename=' . $file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $file_size);
ob_clean();
flush();
readfile($file_path);
exit;
I hope, this tutorial has helped you a lot in undertanding the concept of Forde File download using PHP.
10 Big reasons Why You Don't Make Money on the Internet How You Can Get Money From The Internet Through Affiliate Marketing Comptia Security+ Certification - Expert Professionals In The Field Of Security And Tech Networking Javafit Review -- Is It Really As Good As It Looks? Online Wealth by Jodi –Review of Internet Cash Secret Discover Your City with Wireless Internet The Real Secret To An Internet Marketing Seed 6 Internet Marketing Myths Pilgimage Tours, Active Holidays And Safari Packages Offered By Internatonal Travel And Tours Unlimited DS Download Scam – Is this website a scam? Enjoy Real Safari Experience In Disneys Animal Kingdom How To Keep The Internet Trustworthy And Enjoyable How To Know If You Should Get Internet Marketing Services