How To Use PHP to Force a File Download
http://webdesign.about.com/od/php/ht/force_download.htm
PHP allows you to change the HTTP headers of files that you’re writing, so that you can force a file to be downloaded that normally the browser would load in the same window. This is perfect for files like PDFs, document files, images, and video that you want your customers to download rather than read online.
Difficulty: Hard
Time Required: 10 minutes
Here’s How:
Upload the file you want to make available for download to your Web server. For example,
huge_document.pdf
Edit a new PHP file – I recommend naming it the same name as your downloaded file, only with the extension .php. For example:
huge_document.php
Open the PHP block:
Your PHP file should look like this:
Link to your PHP file as a download link. For example:
Download my huge document (PDF)
Tips:
There should be no spaces or carriage returns anywhere in the file. Blank lines will cause PHP to default to text/html and your file won’t download.
Comments
775 responses to “使用 PHP 下载文档(PDF)”