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);