Home  >  Article  >  Backend Development  >  How to export files in php

How to export files in php

藏色散人
藏色散人Original
2019-10-10 09:14:573028browse

How to export files in php

How to export files in php?

In PHP programming, if you want to export the query results to Excel, you only need to modify the Context-Type of the page.

For example:

Code example:

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

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 parameter:

attachment --- Download as attachment

inline --- Open online

Specific use:

Code example:

header("Content-Disposition: inline; filename=文件名.mp3");
Header("Content-Disposition:attachment;filename=test.xls");

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 When selecting the file type, you can set Content-Type to binary mode:

Header("Content-type:   application/octet-stream");

Example:

Code example:

<?
$filename   =   &#39;./download/d.rar &#39;;
$filesize   =   filesize($filename);
header( "Content-Type:   application/force-download ");
header( "Content-Disposition:   attachment;   filename= ".basename($filename));
header( "Content-Length:   ".$filesize);
$data   =   file_get_contents($filename);
echo   $data;
?>

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(
    &#39;doc&#39;        => &#39;application/msword&#39;,
    &#39;bin&#39;        => &#39;application/octet-stream&#39;,
    &#39;exe&#39;        => &#39;application/octet-stream&#39;,
    &#39;so&#39;        => &#39;application/octet-stream&#39;,
    &#39;dll&#39;        => &#39;application/octet-stream&#39;,
    &#39;pdf&#39;        => &#39;application/pdf&#39;,
    &#39;ai&#39;        => &#39;application/postscript&#39;,
    &#39;xls&#39;        => &#39;application/vnd.ms-excel&#39;,
    &#39;ppt&#39;        => &#39;application/vnd.ms-powerpoint&#39;,
    &#39;dir&#39;        => &#39;application/x-director&#39;,
    &#39;js&#39;        => &#39;application/x-javascript&#39;,
    &#39;swf&#39;        => &#39;application/x-shockwave-flash&#39;,
    &#39;xhtml&#39;        => &#39;application/xhtml+xml&#39;,
    &#39;xht&#39;        => &#39;application/xhtml+xml&#39;,
    &#39;zip&#39;        => &#39;application/zip&#39;,
    &#39;mid&#39;        => &#39;audio/midi&#39;,
    &#39;midi&#39;        => &#39;audio/midi&#39;,
    &#39;mp3&#39;        => &#39;audio/mpeg&#39;,
    &#39;rm&#39;        => &#39;audio/x-pn-realaudio&#39;,
    &#39;rpm&#39;        => &#39;audio/x-pn-realaudio-plugin&#39;,
    &#39;wav&#39;        => &#39;audio/x-wav&#39;,
    &#39;bmp&#39;        => &#39;image/bmp&#39;,
    &#39;gif&#39;        => &#39;image/gif&#39;,
    &#39;jpeg&#39;        => &#39;image/jpeg&#39;,
    &#39;jpg&#39;        => &#39;image/jpeg&#39;,
    &#39;png&#39;        => &#39;image/png&#39;,
    &#39;css&#39;        => &#39;text/css&#39;,
    &#39;html&#39;        => &#39;text/html&#39;,
    &#39;htm&#39;        => &#39;text/html&#39;,
    &#39;txt&#39;        => &#39;text/plain&#39;,
    &#39;xsl&#39;        => &#39;text/xml&#39;,
    &#39;xml&#39;        => &#39;text/xml&#39;,
    &#39;mpeg&#39;        => &#39;video/mpeg&#39;,
    &#39;mpg&#39;        => &#39;video/mpeg&#39;,
    &#39;avi&#39;        => &#39;video/x-msvideo&#39;,
    &#39;movie&#39;        => &#39;video/x-sgi-movie&#39;, 
);

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to export files in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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