Php header function download file implementation code

Source: Internet
Author: User
Tags bmp image readfile

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 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: Copy code

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: Copy code

<? Php
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: Copy code

<? Php
Header ('HTTP/1.1 404 Not Found ');
Header ("status: 404 Not Found ");
?>

Download


Example

The Code is as follows: Copy code

<? Php
$ 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: Copy code
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

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.