Content-type: How to force a file download

本文介绍了如何使用PHP实现不同文件类型的下载,并列举了多种文件格式及其对应的MIME类型,帮助开发者正确设置HTTP头部信息。
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.
(type) lenovo@lenovo-ThinkStation-P2-Tower:/media/lenovo/E676335D76332E25/SCLC_rna-seq/tumor/HLAtype$ sudo rm /var/crash/nvidia-kernel-source-550* # 删除冲突崩溃报告 (type) lenovo@lenovo-ThinkStation-P2-Tower:/media/lenovo/E676335D76332E25/SCLC_rna-seq/tumor/HLAtype$ sudo rm /var/lib/dkms/nvidia/550.144.03/build/make.log # 清除旧日志 (type) lenovo@lenovo-ThinkStation-P2-Tower:/media/lenovo/E676335D76332E25/SCLC_rna-seq/tumor/HLAtype$ sudo rm /var/lib/dpkg/lock* # 解除APT锁定 (type) lenovo@lenovo-ThinkStation-P2-Tower:/media/lenovo/E676335D76332E25/SCLC_rna-seq/tumor/HLAtype$ sudo apt update 命中:1 http://security.ubuntu.com/ubuntu noble-security InRelease 命中:2 http://archive.ubuntu.com/ubuntu noble InRelease 命中:3 https://download.docker.com/linux/ubuntu noble InRelease 正在读取软件包列表... 完成 正在分析软件包的依赖关系树... 完成 正在读取状态信息... 完成 有 1 个软件包可以升级。请执行 ‘apt list --upgradable’ 来查看它们。 (type) lenovo@lenovo-ThinkStation-P2-Tower:/media/lenovo/E676335D76332E25/SCLC_rna-seq/tumor/HLAtype$ sudo apt --fix-broken install -o Dpkg::Options::="--force-overwrite" 正在读取软件包列表... 完成 正在分析软件包的依赖关系树... 完成 正在读取状态信息... 完成 下列软件包是自动安装的并且现在不需要了: linux-hwe-6.11-tools-6.11.0-25 linux-tools-6.11.0-25-generic 使用'sudo apt autoremove'来卸载它(它们)。 升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 1 个软件包未被升级。 有 2 个软件包没有被完全安装或卸载。 解压缩后会消耗 0 B 的额外空间。 正在设置 nvidia-dkms-550 (550.144.03-0ubuntu0.24.04.1) ... update-initramfs: deferring update (trigger activated) update-initramfs: Generating /boot/initrd.img-6.14.0-28-generic INFO:Enable nvidia DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/lenovo_thinkpad DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/dell_latitude DEBUG:Parsing /usr/share/ubuntu-drivers-common/quirks/put_your_quirks_here Removing old nvidia-550.144.03 DKMS files... Deleting module nvidia-550.144.03 completely from the DKMS tree. Loading new nvidia-550.144.03 DKMS files... Building for 6.14.0-28-generic 6.14.0-29-generic Building for architecture x86_64 Building initial module for 6.14.0-28-generic Error! Bad return status for module build on kernel: 6.14.0-28-generic (x86_64) Consult /var/lib/dkms/nvidia/550.144.03/build/make.log for more information. dpkg: 处理软件包 nvidia-dkms-550 (--configure)时出错: 已安装 nvidia-dkms-550 软件包 post-installation 脚本 子进程返回错误状态 10 正在设置 linux-image-6.14.0-29-generic (6.14.0-29.29~24.04.1) ... 正在处理用于 initramfs-tools (0.142ubuntu25.4) 的触发器 ... update-initramfs: Generating /boot/initrd.img-6.14.0-28-generic 正在处理用于 linux-image-6.14.0-29-generic (6.14.0-29.29~24.04.1) 的触发器 ... /etc/kernel/postinst.d/dkms: * dkms: running auto installation service for kernel 6.14.0-29-generic Sign command: /usr/bin/kmodsign Signing key: /var/lib/shim-signed/mok/MOK.priv Public certificate (MOK): /var/lib/shim-signed/mok/MOK.der Building module: Cleaning build area... unset ARCH; [ ! -h /usr/bin/cc ] && export CC=/usr/bin/gcc; env NV_VERBOSE=1 'ma ke' -j16 NV_EXCLUDE_BUILD_MODULES='' KERNEL_UNAME=6.14.0-29-generic IGNORE_XEN_P RESENCE=1 IGNORE_CC_MISMATCH=1 SYSSRC=/lib/modules/6.14.0-29-generic/build LD=/u sr/bin/ld.bfd CONFIG_X86_KERNEL_IBT= modules.........(bad exit status: 2) ERROR: Cannot create report: [Errno 17] File exists: '/var/crash/nvidia-kernel-s ource-550.0.crash' Error! Bad return status for module build on kernel: 6.14.0-29-generic (x86_64) Consult /var/lib/dkms/nvidia/550.144.03/build/make.log for more information. dkms autoinstall on 6.14.0-29-generic/x86_64 failed for nvidia(10) Error! One or more modules failed to install during autoinstall. Refer to previous errors for more information. * dkms: autoinstall for kernel 6.14.0-29-generic ...fail! run-parts: /etc/kernel/postinst.d/dkms exited with return code 11 dpkg: 处理软件包 linux-image-6.14.0-29-generic (--configure)时出错: 已安装 linux-image-6.14.0-29-generic 软件包 post-installation 脚本 子进程返回错 误状态 11 在处理时有错误发生: nvidia-dkms-550 linux-image-6.14.0-29-generic E: Sub-process /usr/bin/dpkg returned an error code (1)
09-03
内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值