Ways to export and download files from PHP

Source: Internet
Author: User
Tags php example
    1. Header ("Content-type:application/vnd.ms-excel")
Copy Code

If you want to be able to provide that open/Save dialog box, the Content-disposition parameter, the content-disposition parameter is intended to provide a suggested file name when the client saves the file, but for security reasons, This parameter is removed from the specification.

Content-disposition parameter: Attachment---as an accessory download inline---open online

Specific use:

    1. Header ("Content-disposition:inline; filename= file name. mp3 ");
    2. Header ("Content-disposition:attachment;filename=test.xls");
Copy Code

In fact, IE is based on content-disposition filename in this section of the suffix to identify the file type, if there are many types of file, you can set the Content-type to binary mode: Header (" Content-type:application/octet-stream ");

Example:

    1. $filename = './download/d.rar ';
    2. $filesize = FileSize ($filename);
    3. Header ("Content-type:application/force-download");
    4. Header ("content-disposition:attachment; Filename= ". basename ($filename));
    5. Header ("Content-length:". $filesize);
    6. $data = file_get_contents ($filename);
    7. Echo $data;
    8. ?>
Copy Code

The above code implementation opens the page immediately after the download saves the window, the download file is $filename.

Some common mimetype types:

$mimetypes = Array (' Doc ' = ' application/msword ', ' bin ' = ' application/octet-stream ', ' exe ' = ' application/oc ') Tet-stream ', ' so ' = ' application/octet-stream ', ' dll ' = ' application/octet-stream ', ' pdf ' = ' application/ PDF ', ' ai ' = ' application/postscript ', ' xls ' = ' application/vnd.ms-excel ', ' ppt ' = ' application/' Vnd.ms-powerpoint ', ' dir ' = ' application/x-director ', ' js ' = ' application/x-javascript ', ' swf ' = ' Application/x-shockwave-flash ', ' xhtml ' = ' application/xhtml+xml ', ' xht ' = ' application/xhtml+xml ', ' zip ' = > ' Application/zip ', ' mid ' = ' Audio/midi ', ' midi ' = ' audio/midi ', ' mp3 ' = ' audio/mpeg ', ' rm ' = ' audio/x ' -pn-realaudio ', ' rpm ' = ' audio/x-pn-realaudio-plugin ', ' wav ' = ' audio/x-wav ', ' bmp ' = ' image/bmp ', ' gif ' =&gt ; ' Image/gif ', ' jpeg ' = ' image/jpeg ', ' jpg ' = ' image/jpeg ', ' png ' = ' image/png ', ' css ' = ' text/css ', ' HTML ' = ' text/html ', ' htm ' = ' text/html ', ' txt ' = ' text/'Plain ', ' xsl ' = ' text/xml ', ' xml ' = ' text/xml ', ' mpeg ' = ' video/mpeg ', ' mpg ' = ' video/mpeg ', ' avi ' = ' Video/x-msvideo ', ' movie ' = ' Video/x-sgi-movie ',);

>>> you may be interested in the article: PHP header function File download directly prompt to save good php header download function php get_headers detection URL is valid method PHP header function Usage example PHP Example of the use of the header function (2) PHP header function Usage example (1) PHP header using a detailed PHP header information application example PHP use header send various types of file Download example PHP header message in detail

  • Related Article

    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.