Home  >  Article  >  Backend Development  >  How to use PHP to force file download_PHP Tutorial

How to use PHP to force file download_PHP Tutorial

WBOY
WBOYOriginal
2016-07-14 10:06:501059browse

PHP allows you to change the HTTP headers you write so that you can force a file to be downloaded, and normally the browser would load the file in the same window. This is perfect for files such as PDF files, document files, images, videos that you want your customers to download rather than read online.
The specific method is as follows:

1. Upload files you want to download to your web server. For example,
huge_document.pdf
2. Edit the new PHP file - I recommend naming it the same as the file you downloaded, just with the PHP extension. For example:
huge_document.php
3. Open the PHP module:
4. In the next line, set the HTTP header:
header('Content-disposition: attachment; filename=huge_document.pdf');
5. Then set the MIME type of the file:
header('Content-type: application/pdf');
6. Point to the file you want to download:
readfile('huge_document.pdf');
7. Then close the PHP block and save the file:
> [/ BLOCKQUOTE>
8. Your PHP file should look like this:
header('Content-disposition: attachment; filename=huge_document.pdf');
header('Content-type: application/pdf');
readfile('huge_document.pdf');
?>
9. Link to the download link of your PHP file. For example:
Download huge document (PDF)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477926.htmlTechArticlePHP allows you to change the browser you write so that you can force a file to be downloaded, normally Will load the HTTP headers of the file in the same window. This is the perfect document like...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn