Home > Backend Development > PHP Tutorial > How to export and download files in php

How to export and download files in php

WBOY
Release: 2016-07-25 08:54:31
Original
1006 people have browsed it
  1. header("Content-Type: application/vnd.ms-excel")
Copy code

If you want to be able to provide the open/save dialog box, Content-Disposition parameter, Content-Disposition The parameter was originally intended to provide a suggested file name when the client saves a file, but for security reasons, this parameter was removed from the specification.

Content-Disposition parameters: attachment --- download as attachment inline --- Open online

Specific use:

  1. header("Content-Disposition: inline; filename=filename.mp3");
  2. Header("Content-Disposition: attachment;filename=test.xls");
Copy code

In fact, IE identifies this file type based on the suffix of the file name in the filename section of Content-Disposition. If there are many file types, you can set 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 the code

The above code realizes that the download and save window will appear immediately after opening the page, and the downloaded file is $filename.

Some commonly used 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-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' => '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', );

>>>> Articles you may be interested in: When downloading the php header function file, you will be prompted to save it directly Easy to use php header download function php get_headers method to detect whether the URL is valid PHP header function usage example php header function usage detailed example (2) php header function usage example (1) Detailed explanation of php header usage php header information application example Examples of php using headers to send various types of files for download Detailed explanation of HEADER header message in PHP



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template