Solution of file_exists function in PHP does not support Chinese name

  • 2021-07-09 07:32:13
  • OfStack

1 Generally speaking, file_exists () is often used in PHP to judge whether a file or folder exists. If it exists, it returns true, otherwise it returns false. However, this function in the web page using UTF8 encoding case, for the Chinese file name or folder name can not return the correct value, always return false. After testing, the solution is obtained, and the reason for this situation should be that PHP can't be judged correctly due to different codes.

The following code is not able to return the correct value, regardless of whether the file is present or not:


<?php;
$file="/attachment/21/0/ Chinese .rar";
$newfile = dirname(__FILE__).$file;

echo file_exists($newfile);
?>

After testing, a sentence is added to convert UTF8 code into GB2312 code, which can be judged correctly:


<?php
$file="/attachment/21/0/ Chinese .rar";
$newfile = dirname(__FILE__).$file;

$file=iconv('UTF-8','GB2312',$file);

echo file_exists($newfile);
?>

Related articles: