Heim  >  Artikel  >  php教程  >  解决PHP中file_exists()判断中文文件名无效

解决PHP中file_exists()判断中文文件名无效

WBOY
WBOYOriginal
2016-05-24 12:59:101219Durchsuche

php中判断文件是否存在我们会使用file_exists函数或is_file函数,但在使用file_exists时如果你文件名或路径是中文在uft8编码文档是是无效的那么如何解决此问题呢,下面我们一起来看看.

定义和用法:

file_exists() 函数检查文件或目录是否存在,如果指定的文件或目录存在则返回 true,否则返回 false.

PHP实例代码如下:

<?php 
echo file_exists("test.txt"); 
 
//输出:1 
PHP实例例二,代码如下:
$realname=&#39;中文.txt&#39;; 
if(file_exists($realname)) { 
   // 永远都进不了这里 
} 
else 
{ 
    echo &#39;www.phprm.com 提醒你文件不存在了&#39;; 
}

//输出结果是 www.phprm.com 提醒你文件不存在了 

但我很惊讶呀文件是存在了,并且路径也没有问题php文件与中文.txt在同一目录所以这样写是没有问题,于时想想会不会是中文问题于,我对编码进行转换.

解决方案,代码如下:

$realname=&#39;中文.txt&#39;; 
 
if(file_exists(iconv(&#39;UTF-8&#39;,&#39;GB2312&#39;,$realname))) { 
   // 这样就可以支持了 
}

结果显示1,问题解决了,在php中最好不要使用中文名字,像apache,linux,php这些对中文支持不怎么好,所以大家尽量使用英文.


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn