首页 > 后端开发 > PHP问题 > php怎么导出文件

php怎么导出文件

藏色散人
发布: 2023-02-25 22:50:01
原创
3067 人浏览过

php怎么导出文件

php怎么导出文件?

在php编程中,如果要将查询结果导出到Excel,只需对页面的Context-Type做修改即可。

例如:

代码示例:

header("Content-Type: application/vnd.ms-excel")
登录后复制

如果希望能够提供那个打开/保存的对话框,Content-Disposition参数,Content-Disposition参数本来是为了在客户端另存文件时提供一个建议的文件名,但是考虑到安全的原因,就从规范中去掉了这个参数。

Content-Disposition参数:

attachment --- 作为附件下载

inline --- 在线打开

具体使用:

代码示例:

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

其实IE是根据Content-Disposition中filename这个段中文件名的后缀来识别这个文件类型的,如果有很多种文件类型的时候,可以将Content-Type设置为二进制模式的:

Header("Content-type:   application/octet-stream");
登录后复制

例子:

代码示例:

<?
$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;
?>
登录后复制

以上代码实现打开页面后立即出现下载保存窗口,下载的文件为$filename。

部分常用mimetype类型:

$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;, 
);
登录后复制

更多PHP相关知识,请访问PHP中文网

以上是php怎么导出文件的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板