Home > Article > Backend Development > 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 = './download/d.rar '; $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( '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', );
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!