首頁 > 後端開發 > PHP問題 > php怎麼匯出文件

php怎麼匯出文件

藏色散人
發布: 2023-02-25 22:50:01
原創
3079 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板