用PHP header写的文件下载, 如何实现根据文件类型自动选择 Content-type $filename = $_POST['download_file'];
header('Content-type: ' . 这里应该怎么写);
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
readfile($filename);请问有没有function可以获取文件的MIME类型? 还是必须手写代码去match file extension and MIME type?
header('Content-type: ' . 这里应该怎么写);
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
readfile($filename);请问有没有function可以获取文件的MIME类型? 还是必须手写代码去match file extension and MIME type?
其它根据扩展名来搞,这是最简单的.
$mime_types = array(
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'bmp' => 'image/bmp',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'pict' => 'image/x-pict',
'pic' => 'image/x-pict',
'pct' => 'image/x-pict',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'psd' => 'image/x-photoshop','swf' => 'application/x-shockwave-flash',
'js' => 'application/x-javascript',
'pdf' => 'application/pdf',
'ps' => 'application/postscript',
'eps' => 'application/postscript',
'ai' => 'application/postscript',
'wmf' => 'application/x-msmetafile','css' => 'text/css',
'htm' => 'text/html',
'html' => 'text/html',
'txt' => 'text/plain',
'xml' => 'text/xml',
'wml' => 'text/wml',
'wbmp' => 'image/vnd.wap.wbmp','mid' => 'audio/midi',
'wav' => 'audio/wav',
'mp3' => 'audio/mpeg',
'mp2' => 'audio/mpeg','avi' => 'video/x-msvideo',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime','lha' => 'application/x-lha',
'lzh' => 'application/x-lha',
'z' => 'application/x-compress',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'gzip' => 'application/x-gzip',
'tgz' => 'application/x-gzip',
'tar' => 'application/x-tar',
'bz2' => 'application/bzip2',
'zip' => 'application/zip',
'arj' => 'application/x-arj',
'rar' => 'application/x-rar-compressed','hqx' => 'application/mac-binhex40',
'sit' => 'application/x-stuffit',
'bin' => 'application/x-macbinary','uu' => 'text/x-uuencode',
'uue' => 'text/x-uuencode','latex'=> 'application/x-latex',
'ltx' => 'application/x-latex',
'tcl' => 'application/x-tcl','pgp' => 'application/pgp',
'asc' => 'application/pgp',
'exe' => 'application/x-msdownload',
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'mdb' => 'application/x-msaccess',
'wri' => 'application/x-mswrite',
);
<?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=d:\test_data.xls");$tx='表头';
echo $tx."\n\n";
//输出内容如下:
echo "姓名"."\t";
echo "年龄"."\t";
echo "学历"."\t";
echo "\n";
echo "张三"."\t";
echo "25"."\t";
echo "本科"."\t";
?>可以生成EXCEL,但会弹出另存为对话框.
怎样才可以自动生成这个EXCEL且自动保存到服务器上,不弹出对话框.
把你要下载的东西进行编码转换mb_convert_encoding();
$filemimetype = $mime_types[$subfilename];header('Cache-control: private');
header('Pragma: public');
if(!empty($filemimetype)){
header('Content-Type: ' . $filemimetype);
}else{
header('Content-Type: application/octet-stream');
}
header('Content-Length: ' . filesize($fullpath));
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="' . basename($fullpath) . '"');
flush();
$file = fopen($fullpath, 'r');
while (!feof($file))
{
print fread($file, filesize($fullpath));
flush();
}
fclose($file);