file_exists doesn’t work for me

Ok i’ve run across this problem a few times in my programming life, where file_exists always returns false. I don’t think i’ve ever properly fixed it. Generally I make some crappy work around, which ends up causing more harm than good.

So here’s a small checklist to solve the problem.

  1. Check the file path you are actually sending the function. eg file_exists(BASE_DIR . “/images/image1.jpg”);
    • I always like to do a print_r on whatever is in between the brackets just to make sure i’m not completely inept.
    • For this example i have too many ‘/’ that could be causing me dilemmas.
  2. Check that the folders/files you are accessing don’t have any safe mode restrictions.
  3. If those 2 solutions didn’t work and you have ensured you have passed the correct file path then put a call to the function “clearstatcache()” before the file_exists function.

If you still have problems even after that, then send me a comment and I’ll help you debug it.

Happy Coding

You can leave a response, or trackback from your own site.

3 Responses to “file_exists doesn’t work for me”

  1. granitemountie says:

    This comment applies to a solution you posted November 1, 2007 Titled “Forms submitting twice in Firefox”. I send my eternal gratitude for your post. I have spent hours and hours, days! trying to figure out odd behavior in a web survey. The symptoms were different depending on whether starting a new survey, or “next”ing through, but I now know they were ALL caused by having a src=”” within a tag. Thank you, thank you!! bretto36 for your generous sharing of knowledge!!!

  2. sri20198 says:

    Trying to implement a function that depends on file_exists in a drupal setup. I am checking for an image file which has a path like ‘/sites/all/themes/themename/images/img_var0.gif’. The strange thing is, when i run for file_exists from the local directory (meaning sitting in images directory), it returns true, but when run from the root, it returns false. Also checked on the safe mode restrictions using phpinfo(), it is set to OFF. The directories as well as the files have read permissions for all.

    Don’t know where i am going wrong.

  3. bretto36 says:

    Is this still an issue for you? Have you checked openbasedir restrictions. Generally you can’t access things outside the root directory unless oyu open it up in httpd.conf

Leave a Reply