Home  >  Article  >  Backend Development  >  PHP allows the browser to download txt and other types of files, uc browser imports txt, the browser opens txt 360 speed browser rules tx

PHP allows the browser to download txt and other types of files, uc browser imports txt, the browser opens txt 360 speed browser rules tx

WBOY
WBOYOriginal
2016-07-29 08:55:131746browse

What will happen if you let the browser access a txt file? For example, when accessing localhost/test/test.txt, the browser will directly display the content of the TXT file on the browser instead of downloading it.

But not all files will be read directly on the browser, files like .zip, .doc, etc. will be downloaded directly, and files like .jpg, .png, .txt will be directly downloaded. Read. Sometimes, when files such as txt are relatively large, we do not want the browser to read them directly, which will also put greater pressure on the server. At this time, you can do it by specifying the header information:

		$file = fopen($url, "r");  //打开文件url
		header("Content-Type: application/octet-stream"); //指定mime类型为八进制文件流
		header("Accept-Ranges: bytes");
		header("Accept-Length: ".filesize($url));
		header("Content-Disposition: attachment; filename=$name");  //$name是文件的名字,一般在$url的最后
		echo fread($file,filesize($url));
		fclose($file);
At this time, the file specified by $url can be downloaded instead of being read directly. The above introduces how PHP allows the browser to download txt and other types of files, including downloading txt and browser downloads. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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