Home  >  Article  >  Backend Development  >  Solution to the problem that the file_exists function in PHP does not support Chinese names_PHP Tutorial

Solution to the problem that the file_exists function in PHP does not support Chinese names_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:54813browse

The solution to the problem that the file_exists function in PHP does not support Chinese names

This article mainly introduces the solution to the problem that the file_exists function in PHP does not support Chinese names. It is a very practical skill that requires Friends can refer to it

Generally speaking, file_exists() is often used in PHP to determine whether a file or folder exists. If it exists, it returns true, otherwise it returns false. However, when the web page uses UTF8 encoding, this function cannot return the correct value for Chinese file names or folder names, and always returns false. After testing, we came up with a solution and analyzed that the reason for this situation should be that PHP cannot judge correctly due to different encodings.

The following code is code that cannot return the correct value. It returns that the file is not present regardless of whether it exists:

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

echo file_exists($newfile);
?>

After testing, a sentence is added to convert UTF8 encoding to GB2312 encoding, and the correct judgment can be made:

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

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

echo file_exists($newfile);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/852817.htmlTechArticleThe solution to the problem that the file_exists function in PHP does not support Chinese names. This article mainly introduces the file_exists function in PHP that does not support Chinese names. Famous solutions, very practical skills, friends in need can refer to...
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