php强制下载文件
由于现在的浏览器已经可以识别格式为txt的文档格式,所以如果只是给txt文档做一个文字链接的话,可能只是打开一个新窗口显示txt文件的内容,并不能实现点击下载的目的。We have to do something else.
当然这个问题的解决办法也可以是你将你的txt文件改名为浏览器不认识的文件,比如rar,这样的话点击,浏览器不认识就只好让用户下载了。
下面这个文件就是通过设置header设置文档的格式来实现点击下载的目的,将上个页面传递过来的文件点击可以下载。
$filename = "/somepath/".$_GET[file].".txt"; //要下载的文件名
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($filename));
readfile($filename);
?>
首先:设置Content-Type的值为application/force-download,强制下载
接着第二个header函数设置了要下载的文件,注意这里filename是不包含路径的文件名,所以用basename过滤掉路径名。这个filename的值将来就是点击下载后弹出对话框里面的文件名.
最后就是readfile,将文件流输出到浏览器,这样就实现了txt文件的下载,其他类型的类似。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor
