php 导出txt并下载,php导出与下载文件的方法

本文介绍了如何在PHP中将查询结果导出为Excel文件,通过设置HTTP头`Content-Type`和`Content-Disposition`来实现。示例代码展示了如何创建一个立即弹出下载对话框的页面,同时提供了不同文件类型对应的MIME类型参考。
摘要由CSDN通过智能技术生成

在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   =   './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;

?>

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

部分常用mimetype类型:

$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',

);

>>> 您可能感兴趣的文章:

php header函数文件下载时直接提示保存

好用的php header下载函数

php get_headers检测URL是否有效的方法

PHP header函数用法举例

php header函数用法详解举例(2)

php header函数用法举例(1)

php header 使用详解

php header头信息应用举例

php使用header发送各种类型文件下载的例子

PHP中HEADER头消息详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值