In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This issue of the content of the editor will bring you about php how to achieve txt download files, the article is rich in content and professional analysis and description for you, after reading this article, I hope you can get something.
Php to achieve txt download file method: 1, create a PHP sample file; 2, add "function downloadtxt ($file =") {...} "code; 3, through" downloadtxt ("classmsg"); "way to download the file.
This article operating environment: windows7 system, PHP7.1 version, Dell G3 computer.
How does php implement txt download files?
PHP realizes the generation and download of txt files
Generation of 1.txt
This function is used in multiple data outputs. Each field is separated by a space, and the next data is wrapped directly.
Public function dlfileftxt ($data = array (), $filename= "unknown") {header ("Content-type:application/octet-stream"); header ("Accept-Ranges:bytes"); header ("Content-Disposition:attachment;filename=$filename.txt"); header ("Expires:0"); header ("Cache-Control:must-revalidate,post-check=0,pre-check=0"); header ("Pragma:public") If (! empty ($data)) {foreach ($data as $key= > $val) {foreach ($val as $ck = > $cv) {$data [$key] [$ck] = iconv ("UTF-8", "GB2312", $cv) } $data [$key] = implode ("", $data [$key]);} echo implode ("\ n", $data);} exit () } dlfileftxt (array (1 = > array ("student" = > "Xiao Hong", "class" = > "Class one"), 2 = > array ("student" = > "Lao Huang", "class" = > "Class two"), "Class Information")
Txt file name: class information
Txt content:
Xiao Hong Class one
Lao Huang Class two
Code parsing:
"Content-type:application/octet-stream"
/ / only one binary can be submitted, and only one binary can be submitted. If a file is submitted, only one file can be submitted. There can be only one backend receiving parameter, and it can only be a stream (or byte array).
"Accept-Ranges: bytes"
/ / indicates whether the server supports specified range requests and what type of segmentation requests
"Content-Disposition:attachment;filename=$filename.txt"
/ / of course, the filename parameter can contain path information, but User-Agnet ignores this information and only takes the last part of the path information as the file name. When you use this header message when the response type is application/octet- stream, it means that you don't want to display the content directly, but pop up a "file download" dialog box, and it's up to you to decide whether to "open" or "save".
(note: some browsers will not recognize the garbled character code of the Chinese string and need to force the character encoding UTF-8 of the string to be converted.)
Download 1.txt
Because some browsers can interpret txt files, when you use a browser to access a txt on the server, you will open a new window to view the txt content instead of downloading the file
Function downloadtxt ($file = "") {if (! $file) return false; $filename='/ path/'.$file.'.txt'; / / File path header ("Content-Type: application/force-download"); header ("Content-Disposition: attachment; filename=" .basename ($filename)); readfile ($filename);} downloadtxt ("classmsg")
Code parsing:
"Content-Type: application/force-download"
/ / forced download of file contents
Readfile ()
/ / this function reads the file and writes the contents of the file to the standard output device. The return value is the size of the file read
The above is the editor for you to share the php how to achieve txt download files, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.