|
Location: Web development - PHP License: The Intelliproject Open License (IPOL) Using PHP Headers to Force DownloadPosted by Silviu CarageaDescribe how to force a file download in php |
Skill: IntermediatePosted: 26/11/2008Views: 571Rating: 3.00 /5Popularity: 0.00 |
| Sign Up to vote for this article |
Sometimes, when creating a website, you may encounter a situation in which you would like to allow your users to download one or more files.
Probably most of us think that we simply need to create a link to the file itself on our server. The problem in this simplistic method works different for every browser and platform.
For instance, let's suppose that we have a pdf file and you want to let users to download this file. You can link to this file by simply creating a link:
<a href="myfile.pdf">Download pdf</a>
The problem with this method is that most browsers have the capability to open your pdf files. So, they will automatically load up their Adobe Reader plug-in and in short time will display your pdf content in your browser.
The good way to force the download is to use the php header function. header() is used to send a raw HTTP
header and must be called before any actual output is sent, either by normal
HTML tags, blank lines in a file, or from PHP. It is a very common error to
read code with include(),
or require(),
functions, or another file access function, and have spaces or empty lines that
are output before header() is called. The same problem exists when using
a single PHP/HTML file.
First off all we need to declare an array with the mime types for all the file extensions that we need to download. If you download the article attachment you will find this array with the mimes types for the most used file extensions.
The DownloadFile get the file path as parameter, we check to
make sure that it actually exists before going any further. If the file doesn't
exist we return FALSE.
After that we get some information's about the specified file like: file size, file name, the extension and the mime type,we display the headers and the file content.
You can use the class described above in the following manner:
This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)
| Silviu Caragea
| Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject. He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy. His programming experience includes: - C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL) - Open Source libraries :CURL & Boost - HTML, CSS - Java (SE,ME) - JavaScript, Ajax, Google Web Toolkit (GWT) - Php, MySQL -Oracle, PL SQL - C# .NET -Objective C, IPhone SDK, Cocoa Location: |
Sign up to post message on the article message board!