Take the 2-minute tour ×

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.


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


outputs: bool(false)


outputs: bool(true)

How could it happen? Thanks for any help in advance

share|improve this question
Offtopic: for checking files, I'd advise to use is_file(), as file_exists() checks both files and directories. –  binaryLV Jul 13 '11 at 8:57

7 Answers 7

up vote 0 down vote accepted

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.

share|improve this answer

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

share|improve this answer
riad@riad-desktop:~$ php -r 'print(strlen("content/images/1107_16.jpg"));'

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

share|improve this answer

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


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

share|improve this answer

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

share|improve this answer

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

var_dump ("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.

share|improve this answer
string(45) "content/images/1107_16.jpg" 

should be less chars long try :


to remove spacechars

share|improve this answer

Your Answer


