The phpheader function downloads the file implementation code. In php, the header function is used heavily. the header can not only send the original HTTP header information to the client, but also directly implement file download operations, next, I will introduce the use of the header function in php. the header can not only send the original HTTP header information to the client, but also directly implement file download operations, next we will introduce you to the editor.
The header function is most commonly used for sending http classes instead of downloading.
Jump
It will execute the last one, but it is conditional, for example:
The code is as follows: |
|
Header ('Location: http://www.bKjia. c0m "); Header ('Location: http://www.g.cn '); Header ('Location: http://www.baidu.com '); This will jump to Baidu Header ('Location: http://www.bKjia. c0m'); echo 'House of friends; Header ('Location: http://www.g.cn '); Header ('Location: http://www.baidu.com '); |
This will jump to google
Sending status
Outputs the status value to the browser, which is mainly used for access permission control.
The code is as follows: |
|
Header ('http/1.1 401 unauthorized '); Header ('status: 401 unauthorized '); ?> |
For example, to restrict a user from accessing this page, you can set the status to 404, as shown below, so that the browser shows that the page does not exist.
The code is as follows: |
|
Header ('http/1.1 404 Not Found '); Header ("status: 404 Not Found "); ?> |
Download
Example
The code is as follows: |
|
$ Filename = 'path + actual filename '; // File type Header ('content-type: application/pdf '); // Download the display name Header ('content-Disposition: attachment; filename = "keep file name "'); Readfile ("$ filename "); Exit (); ?> |
The header function is converted accordingly,
The code is as follows: |
|
Header ('content-type: application/octet-stream'); // The output type. select the corresponding type based on the MIME table provided below. Header ('content-Disposition: attachment; filename= ..rar "'); // download the display name Readfile('file name .rar 'on the server ');// |
File to be downloaded, including the path
Common MIME types
. Doc application/msword
. Docx application/vnd.openxmlformats-officedocument.wordprocessingml.doc ument
. 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
HTTP header information, you can also directly download files, the next small series to introduce you...