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