Home >Backend Development >PHP Problem >What should I do if the php file download shows that the file cannot be found?

What should I do if the php file download shows that the file cannot be found?

藏色散人
藏色散人Original
2020-08-27 09:58:012349browse

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.

What should I do if the php file download shows that the file cannot be found?

Recommendation: "PHP Video Tutorial"

Solution for php file download and file_exists cannot find the file

Link:7d8751be63002cae46a2f36bd0c3841dClick to download5db79b134e9f6b82c0b36e0489ee08ed

Among them, php:

<?php
$filename = $_GET[&#39;filename&#39;];
//从浏览器获取到的字符的编码是UTF-8,我们要用这个函数转换成GBK才能才本地找到这个文件
$filename = mb_convert_encoding($filename,&#39;GBK&#39;,&#39;UTF-8&#39;);
echo $filename ."<br>";
if( empty($filename)){
    echo&#39;<script> alert("非法连接 !"); location.replace ("index.php") </script>&#39;; 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!

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