PHP file_exists() anomaly

前提是你 提交于 2019-12-11 03:03:16

问题


i ran into this wierd problem: the $myImg variable has been extracted from some local html and points to a file i would like to check. With the string variable file_exists gives false, but if the content os variable is inserted manually it gives true.

var_dump($myImg);

outputs: string(26) "content/images/1107_16.jpg"

var_dump(file_exists($myImg));

outputs: bool(false)

var_dump(file_exists("content/images/1107_16.jpg"));

outputs: bool(true)

How could it happen? Thanks for any help in advance


回答1:


Try converting the string before passing it to file_exists method

$myImg = mb_convert_encoding($myImg, "UTF-8");

Additionally, you can always trim the other unwanted characters attached to the dirty string.




回答2:


The length of content/images/1107_16.jpg is not 45, so obviously there are other characters. Try trimming the variable.




回答3:


riad@riad-desktop:~$ php -r 'print(strlen("content/images/1107_16.jpg"));'
26

So, Your string is bad (maybe consist of a bad characters (\0 ?) on the end




回答4:


I offer you to compare the results using bin2hex():

var_dump(bin2hex($myImg));
var_dump(bin2hex("content/images/1107_16.jpg"));

It seems that it's the problem of different charsets.




回答5:


perhaps some invisible character in your $myImg? something like a line break?




回答6:


You seem to be have some unprintable characters in your string, or a different character encoding:

var_dump ("content/images/1107_16.jpg");
string(26) "content/images/1107_16.jpg"

Note that this correctly reports 26 bytes - which would be right with ASCII (or UTF-8). The 45 bytes reported above would sem like some sort of multibyte encoding.




回答7:


string(45) "content/images/1107_16.jpg" 

should be less chars long try :

trim($myImg)

to remove spacechars



来源:https://stackoverflow.com/questions/6676295/php-file-exists-anomaly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!