Usage of Content-disposition in phpheader _ PHP Tutorial

Source: Internet
Author: User
The usage of Content-disposition in phpheader is detailed. This article details the usage of Content-disposition in phpheader. For more information about header usage, see. Content-disposition is an extension of the MIME protocol. This article describes in detail the usage of Content-disposition in php headers. For more information about header usage, see.

Content-disposition is an extension of the MIME protocol. The MIME protocol instructs the MIME user agent how to display additional files. Content-disposition can actually control whether the Content requested by the user is saved as a file and provide a default file name. The file is displayed directly in the browser or a file download dialog box is displayed during access.


Format Description:

Content-disposition = "Content-Disposition": "disposition-type * ("; "disposition-parm)

Field description:

Content-Disposition: attribute name

How is disposition-type downloaded, for example, attachment?

Disposition-parm is the default file name for saving

When the server sends a file to the client browser, if the file type is supported by the browser, it is generally opened by default using a browser, such as txt and jpg, and will be directly displayed in the browser, if you need to prompt the user to save, use Content-Disposition for processing. The key is to add attachment:

The code is as follows:

Response. AppendHeader ("Content-Disposition", "attachment?filename=filename.txt ");

Note: In this case, the browser will prompt whether to save or open the file. even if you choose to open the file, it will also use the associated program, such as Notepad, instead of opening the file directly by IE.

Content-Disposition provides a default file name when you want to save the requested Content as a file. The specific definition is as follows:

The code is as follows:

Content-disposition = "Content-Disposition" ":"

Disposition-type * (";" disposition-parm)

Disposition-type = "attachment" | disp-extension-token

Disposition-parm = filename-parm | disp-extension-parm

Filename-parm = "filename" "=" quoted-string

Disp-extension-token = token

Disp-extension-parm = token "=" (token | quoted-string)

The specific example is as follows:

Content-Disposition: attachment; filename=“filename.xls"

Of course, the filename parameter can contain path information, but User-Agnet ignores this information and only uses the last part of the path information as the file name. If you use this header when the response type is application/octet-stream, it means that you do not want to directly display the content, but instead bring up a "file download" dialog box, the next step is to decide whether to "open" or "save.

Note:

1. when Content-Disposition is used in the code to ensure that the download dialog box is displayed in the browser. Response. addHeader ("Content-Disposition", "attachment"); make sure that you have not performed any operations to prohibit browser caching. As follows:

The code is as follows:

Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "No-cache ");
Response. setDateHeader ("Expires", 0 );

Otherwise, you will find that the download function is okay in opera and firefox, but not in IE.

The usage of Content-disposition in the response header is detailed. if you need to know the usage of the header, refer to it. Content-disposition is an extension of the MIME protocol ,...

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.