Download File Problem in Php (Forcing File to Download instead Of Opening It In Browser)

Php is used very widespread these days. CMSs’ like Joomla, WordPress, Drupal are based on Php only. Because of it is open source, people are using it more.

I was trying to implement Download link in my application. Simply I was giving a link to my file in an anchor tag and trying to download. But instead of downloading that file from server to my local system , It was opening the file in the browser itself. I tried for image, .txt file but I was not able to download.Check out screenshots. If the browser is capable of opening files then it might open it instead of downloading if you simply provide a link of the file. You have explicitly write a script to download file forcefully instead of opening it.

 

(index.php)

 

 

(File Opened in Browser)
This Php script rescued me and I was able to download files from the server. Download code from here :- http://www.4shared.com/zip/YIIy-d1j/Download.html

From file, we have to pass the exact file name as an argument, so the script will get that file name and force the browser to download the file instead of opening it in the browser. Check out how to pass file name in the screenshot.
(Download URL)

In this script, We are extracting the file name from the path using string operations. Later on, we are checking that whether this file exists on the server or not. If it exists then we are forcing it to download instead of opening it in the browser using the marked code in the screenshot.

(Download Script)
(File Downloaded instead of opening in browser)
Check out Tutorial on How to Develop Self-Destruct File Sharing Portal : http://www.technonutty.com/2013/05/develop-self-destruct-file-sharing.html

Leave a Reply

Your email address will not be published. Required fields are marked *