Rumah > pembangunan bahagian belakang > masalah PHP > php怎么导出文件

php怎么导出文件

藏色散人
Lepaskan: 2023-02-25 22:50:01
asal
3051 orang telah melayarinya

php怎么导出文件

php怎么导出文件?

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

例如:

代码示例:

header("Content-Type: application/vnd.ms-excel")
Salin selepas log masuk

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

Content-Disposition参数:

attachment --- 作为附件下载

inline --- 在线打开

具体使用:

代码示例:

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

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

Header("Content-type:   application/octet-stream");
Salin selepas log masuk

例子:

代码示例:

<?
$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;
?>
Salin selepas log masuk

以上代码实现打开页面后立即出现下载保存窗口,下载的文件为$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;, 
);
Salin selepas log masuk

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

Atas ialah kandungan terperinci php怎么导出文件. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan