intelliproject logo

Location: Web development - PHP    License: The Intelliproject Open License (IPOL)

Using PHP Headers to Force Download

Posted by Silviu Caragea

Describe how to force a file download in php

Skill: Intermediate

Posted: 26/11/2008

Views: 571

Rating: 3.00 /5

Popularity: 0.00

Sign Up to vote for this article

Introduction

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.

The source code

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:

License

This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)

About the author

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: Romania
Ocupation: Software Engineer
Home page: http://www.intelliproject.net

Posted by Ahmed Magdy Ezzeldin at 07/12/2008 12:32
To make it simple the minimal lines you really need are:

# header("Content-Disposition: attachment; filename=".$filename.";");
# header("Content-Length: ".filesize($file));
# readfile($file);
# exit;

Where $file is the path to your file and $filename is the user friendly file name.

Sign up to post message on the article message board!