Search code examples
phpfile-exists

PHP file_exists() anomaly


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


Solution

  • 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.