Download file in PHP is a very simple process. If you need to download a file forcefully in PHP it’s okay. But sometimes the corrupted file or some other format file cause problems in downloading. So today I am going to tell you how to forcefully download a file in PHP.
We need to force the browser to download the file. Mainly we use header() and read file() PHP function to forcefully download the file. Here I have given an example of how to download a file forcefully. You easily can download a file from a directory. Also, you may download any kind of file like image, Document, PSD, ZIP, etc.
Download in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$yourfileName = basename('thecodehelpers.txt'); $yourfilePath = 'files/'.$yourfileName; if(!empty($yourfileName) && file_exists($yourfilePath)){ // here we have define headers function parametes header("Content-Description: File Transfer"); header("Cache-Control: public"); header("Content-Type: application/zip"); header("Content-Disposition: attachment; filename=$yourfileName"); header("Content-Transfer-Encoding: binary"); // here it is reading file readfile($filePath); exit; }else{ echo 'The file is not exist.'; } ?> |
ReadFile() Function
This is an inbuilt function of PHP. The main use of this function is to read the file and store it temporarily in an output buffer. The filename will be sent as a parameter to the readfile() and it return number of the byte is read in a success state.
Download a File Using Anchor Link
This is the crucial and important part when the user wants to save a file forcefully. Sometimes you need to give a link to the user to save a file from the directory using PHP.
1 |
<a href="downloadfile.php?file=thecodehelpers.png">Click Here To Dowload File</a> |
Download.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php if(!empty($_GET['downfile'])){ $yourfileName = basename($_GET['downfile']); $yourfilePath = 'files/'.$yourfileName; if(!empty($yourfileName) && file_exists($yourfilePath)){ // Define page headers header("Content-Description: File Transfer"); header("Cache-Control: public"); header("Content-Disposition: attachment; filename=$yourfileName"); header("Content-Transfer-Encoding: binary"); header("Content-Type: application/zip"); // Read given file readfile($yourfilePath); exit; }else{ echo 'The file vailable.'; } } ?> |
That’s it for today and thanks for reading our post. Please comment if you really like this article and get help from the information we shared. If you like this make sure you subscribe to our newsletter services so that you can receive notifications for future updates and articles. And if you still have any query you can ask us just by commenting below.
You can also read these article: How to Convert Image to Greyscale Image in PHP
How to Redirect Any URL to Https in PHP using htaccess File
Send Push Notification to Android and iOS App Using PHP and Firebase
this is helpful to me i am bigginer in php from kolkatta Thanks thecodehelpers
Thank you
Hi there mates, fastidious post and nice arguments commented here, I am really enjoying by these.
Great web site. Lots of useful info here. I’m sending it to several buddies ans also sharing in delicious.
And naturally, thank you in your effort!
Hey There. I found your blog using msn. This is an extremely
well written article. I’ll be sure to bookmark it and come back to read more of your useful
information. Thanks for the post. I will definitely return.