溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php導(dǎo)出文件的方法

發(fā)布時間:2020-09-18 12:24:43 來源:億速云 閱讀:201 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)php導(dǎo)出文件的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

php怎么導(dǎo)出文件?

在php編程中,如果要將查詢結(jié)果導(dǎo)出到Excel,只需對頁面的Context-Type做修改即可。

例如:

代碼示例:

header("Content-Type: application/vnd.ms-excel")

如果希望能夠提供那個打開/保存的對話框,Content-Disposition參數(shù),Content-Disposition參數(shù)本來是為了在客戶端另存文件時提供一個建議的文件名,但是考慮到安全的原因,就從規(guī)范中去掉了這個參數(shù)。

Content-Disposition參數(shù):

attachment --- 作為附件下載

inline --- 在線打開

具體使用:

代碼示例:

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

其實IE是根據(jù)Content-Disposition中filename這個段中文件名的后綴來識別這個文件類型的,如果有很多種文件類型的時候,可以將Content-Type設(shè)置為二進(jìn)制模式的:

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;
?>

以上代碼實現(xiàn)打開頁面后立即出現(xiàn)下載保存窗口,下載的文件為$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', 
);

關(guān)于php導(dǎo)出文件的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI