Home php教程 php手册 用PHP实现浏览器点击下载TXT文档的方法详解

用PHP实现浏览器点击下载TXT文档的方法详解

Jun 13, 2016 am 11:52 AM
php txt download Can accomplish document method Browser Click now use of identify Detailed explanation

由于现在的浏览器已经可以识别txt文档格式,如果只给txt文档做一个文字链接的话,点击后只是打开一个新窗口显示txt文件的内容,并不能实现点击下载的目的。当然这个问题的解决办法也可以是将txt文件改名为浏览器不认识的文件(比如rar),这样的话,由于浏览器不能识别rar类型的文件,只能让用户下载了。还有一种办法,就是利用代码通过header设置文档的格式来实现点击下载的目的。
PHP代码如下:
===========================================================
$filename = '/path/'.$_GET['file'].'.txt'; //文件路径
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($filename));
readfile($filename);
===========================================================
简要说明:
第一个header函数设置Content-Type的值为application/force-download;
第二个header函数设置要下载的文件。注意这里的filename是不包含路径的文件名,filename的值将来就是点击下载后弹出对话框里面的文件名,如果带路径的话,弹出对话框的文件名就是未知的;
最后通过readfile函数,将文件流输出到浏览器,这样就实现了txt文件的下载。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What is the reason for redirecting 404 errors after logging in with Selenium? How to solve it? What is the reason for redirecting 404 errors after logging in with Selenium? How to solve it? Apr 01, 2025 pm 10:54 PM

Solution to Redirecting 404 Errors after Simulation Login When using Selenium for Simulation Login, we often encounter some difficult problems. �...

How to implement cross-application jump for Word plug-in login authorization? How to implement cross-application jump for Word plug-in login authorization? Apr 01, 2025 pm 11:27 PM

How to implement cross-application jump for Word plug-in login authorization? When using certain Word plugins, we often encounter this scenario: click on the login in the plugin...

The Python subprocess module fails to execute the wmic datafile command. How to solve it? The Python subprocess module fails to execute the wmic datafile command. How to solve it? Apr 01, 2025 pm 08:48 PM

Use Python's subprocess module to execute wmic...

Jiutian Computing Power Platform Task: Will the computing task continue to run after the local computer is shut down? Jiutian Computing Power Platform Task: Will the computing task continue to run after the local computer is shut down? Apr 01, 2025 pm 11:57 PM

Discussion on the task status after the local computer of Jiutian Computing Power Platform is closed. Many users will encounter a question when using Jiutian Computing Power Platform for artificial intelligence training...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

How to jump from Word plug-in to browser for login authorization? How to jump from Word plug-in to browser for login authorization? Apr 01, 2025 pm 08:27 PM

How to achieve login authorization from within the application to outside the application? In some applications, we often encounter the need to jump from one application to another...

See all articles