Php file export and download methods

Source: Internet
Author: User
Php file export and download methods

  1. Header ("Content-Type: application/vnd. ms-excel ")

If you want to provide the open/save dialog box, the Content-Disposition parameter and the Content-Disposition parameter are intended to provide a recommended file name when the client saves another file, however, this parameter is removed from the specification for security reasons.

Content-Disposition parameter: attachment --- download inline as attachment --- open online

Specific use:

  1. Header ("Content-Disposition: inline; filename ");
  2. Header ("Content-Disposition: attachment?filename=test.xls ");

In fact, IE identifies the file type based on the filename suffix in the Content-Disposition segment. if there are many file types, you can set Content-Type to binary: 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. ?>

The download and save window appears immediately after the preceding code is opened. the downloaded file is $ filename.

Some common mimetype types:

$ Mimetypes = array ('Doc' => 'application/msword', 'bin' => 'application/octet-stream ', 'exe '=> 'application/octet-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-ctor', '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' => '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 ', 'Av' => 'video/x-msvideo ', 'movie '=> 'video/x-sgi-movie ',);

>>> Articles you may be interested in: when downloading the php header function file, you are prompted to save the useful php header download function. php get_headers checks whether the URL is valid. PHP header function Usage example: php header function usage example (2) php header function usage example (1) php header usage example php header information application example php sending various types of file downloads example PHP header message explanation

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.