May 11, 2013

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

Posted by Nevil patel
Php is used very widespreadly 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 link of my file in anchor tag and trying to download. But instead of downloading that file from server to my local system , It was opening the file in browser itself. I tried for image, .txt file but I was not able to download.Check out screenshots. If browser is capable of opening files then it might open it instead of downloading if you simply provide link of file. You have explicitly write 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 server.
From file we have to pass the exact file name as argument, so the script will get that file name and force browser to download file instead of opening it in browser. Check out how to pass file name in screenshot.


(Download URL)

In this script, We are extracting file name from path using string operations. Later on we are checking that whether this file exists on server or not. If it exists then we are forcing it to download instead of opening it in browser using marked code in 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

0 comments:

Post a Comment