The solution to the problem that the php file download shows that the file cannot be found: first open the corresponding download code file; then obtain the character encoding from the browser; then use the "mb_convert_encoding" function to convert the encoding; finally use "file_exists" Just download the function implementation file.
Recommendation: "PHP Video Tutorial"
Solution for php file download and file_exists cannot find the file
Link:Click to download
Among them, php:
<?php $filename = $_GET['filename']; //从浏览器获取到的字符的编码是UTF-8,我们要用这个函数转换成GBK才能才本地找到这个文件 $filename = mb_convert_encoding($filename,'GBK','UTF-8'); echo $filename ."<br>"; if( empty($filename)){ echo'<script> alert("非法连接 !"); location.replace ("index.php") </script>'; exit(); } if (!file_exists($filename)) { //检查文件是否存在 echo "文件找不到"; exit; } else { $file = fopen($filename,"r"); // 打开文件 // 输入文件标签 Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length: ".filesize($filename)); Header("Content-Disposition: attachment; filename=" . $filename); // 输出文件内容 echo fread($file,filesize($filename)); fclose($file); exit(); } ?>
Summary: If the file address passed in by the browser is not transcoded (from UTF-8 to GBK), then the file_exists function will not be able to find the file with a Chinese name.
The above is the detailed content of What should I do if the php file download shows that the file cannot be found?. For more information, please follow other related articles on the PHP Chinese website!