Content-type: How to force a file download

Word Document:
PHP Code:
<!-- php buffer start --> $filename = "secure/writeToFile.doc";

header("Content-Length: " . filesize($filename));
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename=writeToFile.doc');

readfile($filename);
<!-- php buffer end -->
MPG / MPEG:
PHP Code:
<!-- php buffer start --> header("Content-Length: " . filesize($filename));
header('Content-Type: video/mpg');
header('Content-Disposition: attachment; filename=test2.mpg');
<!-- php buffer end -->
Others just change the Content-Type:
PHP Code:
<!-- php buffer start --> "pdf": "application/pdf"
"exe": "application/octet-stream"
"zip": "application/zip"
"xls": "application/vnd.ms-excel"
"ppt": "application/vnd.ms-powerpoint"
"gif": "image/gif"
"png": "image/png"
"jpg": "image/jpg"
"mp3": "audio/mpeg"
"mp3": "audio/mp3"
"wav": "audio/x-wav"
"mpe": "video/mpeg"
"mov": "video/quicktime"
"avi": "video/x-msvideo"
<!-- php buffer end -->
ENJOY!

*Note:* Just like all web aspects, Internet Explorer has some issues. If if doesn't not want to play with IE, download the file first and then play it locally. This fixes most issues.

**Side note** Notice in my example, all of my files reside in directory "secure." This directory has the mod permissions of 700 (User r/w/e | Group "none" | Other "none" ). This is how you can secure or make people login into to view or download you files.

EDIT: more mime-types;
PHP Code:
<!-- php buffer start --> application/andrew-insetez
application/mac-binhex40hqx
application/mac-compactprocpt
application/mathml+xmlmathml
application/msworddoc
application/octet-streambin dms lha lzh exe class so dll dmg
application/odaoda
application/oggogg
application/pdfpdf
application/postscriptai eps ps
application/rdf+xmlrdf
application/smilsmi smil
application/srgsgram
application/srgs+xmlgrxml
application/vnd.mifmif
application/vnd.mozilla.xul+xmlxul
application/vnd.ms-excelxls
application/vnd.ms-powerpointppt
application/vnd.wap.wbxmlwbxml
application/vnd.wap.wmlc.wmlc wmlc
application/vnd.wap.wmlscriptc.wmlsc wmlsc
application/voicexml+xmlvxml
application/x-bcpiobcpio
application/x-cdlinkvcd
application/x-chess-pgnpgn
application/x-cpiocpio
application/x-cshcsh
application/x-directordcr dir dxr
application/x-dvidvi
application/x-futuresplashspl
application/x-gtargtar
application/x-hdfhdf
application/x-httpd-php.php .php4 .php3 .phtml
application/x-httpd-php-source.phps
application/x-javascriptjs
application/x-koanskp skd skt skm
application/x-latexlatex
application/x-netcdfnc cdf
application/x-pkcs7-crl.crl
application/x-shsh
application/x-sharshar
application/x-shockwave-flashswf
application/x-stuffitsit
application/x-sv4cpiosv4cpio
application/x-sv4crcsv4crc
application/x-tar.tgz tar
application/x-tcltcl
application/x-textex
application/x-texinfotexinfo texi
application/x-trofft tr roff
application/x-troff-manman
application/x-troff-meme
application/x-troff-msms
application/x-ustarustar
application/x-wais-sourcesrc
application/x-x509-ca-cert.crt
application/xhtml+xmlxhtml xht
application/xmlxml xsl
application/xml-dtddtd
application/xslt+xmlxslt
application/zipzip
audio/basicau snd
audio/midimid midi kar
audio/mpegmpga mp2 mp3
audio/x-aiffaif aiff aifc
audio/x-mpegurlm3u
audio/x-pn-realaudioram rm
audio/x-pn-realaudio-pluginrpm
audio/x-realaudiora
audio/x-wavwav
chemical/x-pdbpdb
chemical/x-xyzxyz
image/bmpbmp
image/cgmcgm
image/gifgif
image/iefief
image/jpegjpeg jpg jpe
image/pngpng
image/svg+xmlsvg
image/tifftiff tif
image/vnd.djvudjvu djv
image/vnd.wap.wbmp.wbmp wbmp
image/x-cmu-rasterras
image/x-iconico
image/x-portable-anymappnm
image/x-portable-bitmappbm
image/x-portable-graymappgm
image/x-portable-pixmapppm
image/x-rgbrgb
image/x-xbitmapxbm
image/x-xpixmapxpm
image/x-xwindowdumpxwd
model/igesigs iges
model/meshmsh mesh silo
model/vrmlwrl vrml
text/calendarics ifb
text/csscss
text/html.shtml html htm
text/plainasc txt
text/richtextrtx
text/rtfrtf
text/sgmlsgml sgm
text/tab-separated-valuestsv
text/vnd.wap.wml.wml wml
text/vnd.wap.wmlscript.wmls wmls
text/x-setextetx
video/mpegmpeg mpg mpe
video/quicktimeqt mov
video/vnd.mpegurlmxu m4u
video/x-msvideoavi
video/x-sgi-moviemovie
x-conference/x-cooltalkice
<!-- php buffer end -->
Additionally,

If you need to find the correct headers for other documents type, email yourself the particular document. Then notice (in Yahoo for instance) what it displays as the header. This way you can get any headers you need.

你可能感兴趣的:(PHP,Excel,IE,XSL,WAP)