PHP Header function download file implementation code

Source: Internet
Author: User
Tags bmp image rar readfile

In PHP, the use of the header function is very large, the header can not only send the original HTTP header information to the client, but also can directly implement the file download operation

The header function is most commonly used not for downloading but for sending HTTP classes.

Jump

It will execute the last one, but conditionally, for example:

Header ('location:http://www.111cn.net '); header (' location:http://header(' Location : Http://www.baidu.com '); // this will jump to Baidu

Send status

Output status value to browser, primarily for access control

<?  Header(' http/1.1 401 Unauthorized 'header(' status:401 unauthorized '?) >

For example, to restrict a user from accessing the page, you can set the status to 404, as shown below, so that the browser is displayed as if the page does not exist

<?  Header(' http/1.1 404 Not Found 'header("status:404 not Found"?>

Download
<?  $filename = ' path + actual filename '//header(' content-type:application/ PDF '//header(' content-disposition:attachment; filename= ' file name when saving. pdf "'   ReadFile("$filename"exit?>

The header function is transformed accordingly
Header (' Content-type:application/octet-stream '); // type of output, depending on the MIME table provided below, select the appropriate type Header (' content-disposition:attachment; filename= "Download display name. rar"); // Download the displayed name ReadFile (' File name on the server. rar '); //Output file contents

commonly used MIME types
. doc Application/msword
. docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
. rtf Application/rtf
. xls Application/vnd.ms-excel Application/x-excel
. xlsx Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
. ppt Application/vnd.ms-powerpoint
. pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
. PPS Application/vnd.ms-powerpoint
. PPSX Application/vnd.openxmlformats-officedocument.presentationml.slideshow
. pdf Application/pdf
. SWF Application/x-shockwave-flash
. dll Application/x-msdownload
. exe Application/octet-stream
. msi Application/octet-stream
. CHM Application/octet-stream
. cab Application/octet-stream
. ocx Application/octet-stream
. rar Application/octet-stream
. Tar Application/x-tar
. tgz application/x-compressed
. zip application/x-zip-compressed
. z application/x-compress
. wav Audio/wav
. wma audio/x-ms-wma
. wmv Video/x-ms-wmv
. mp3. mp2. mpe. mpeg. MPG audio/mpeg
. RM Application/vnd.rn-realmedia
. Mid. Midi. RMI Audio/mid
. bmp Image/bmp
. gif image/gif
. png image/png
. tif. TIFF Image/tiff
. jpe. jpeg. jpg image/jpeg
. txt Text/plain
. xml Text/xml
. html text/html
. CSS Text/css
. JS Text/javascript

PHP Header function download file implementation code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.