I’m having a strange problem with the
file_exists() PHP function on a Linux server.
When using file_exists to check the image is on the server before editing it, it will sometimes fail for no apparent reason. File paths are absolute and I’ve checked them through ssh they are 100% at the specified path.
The images that fail file_exists() will still display in the browser. The code doesn’t make a difference a basic
var_dump(file_exists('/home/user/path/image.jpg')); will return false.
The file permissions/ownerships are exactly the same on all images and parent directories. Safe mode is off. There are no PHP errors. I’m stumped now and not sure what to look for.
Here is how the images are on the server:
/home/user/public_html/images/location/1.jpg -- will work /home/user/public_html/images/location2/1.jpg -- won't work /home/user/public_html/images/location2/2.jpg -- will work
I have root access to the server so if anyone has any ideas I will happily try them out.
Thanks in advance for your advice, let me know if you need extra info.
To answer the questions in the comments below.
- Yes the images are there, it’s definitely not caching.
- Safe mode is off (I already mentioned this in my post).
- Paths are not symlinks but direct links to the files location on the server.
- Phoenix, it is consistent. The working files will always work and vice versa. The names are 1.jpg, 1.thumb.jpg, etc. so it’s not a problem with strange characters or spaces.
[[email protected] ~]# ls -l /home/user/public_html/images/Hawkhurst/ total 92 -rwxr-xr-x 1 user user 24501 Aug 11 2009 1.jpg -rwxr-xr-x 1 user user 1672 Aug 11 2009 1.thumb.jpg -rwxr-xr-x 1 user user 14983 Aug 11 2009 2.jpg -rwxr-xr-x 1 user user 1370 Aug 11 2009 2.thumb.jpg -rwxr-xr-x 1 user user 17238 Aug 11 2009 3.jpg -rwxr-xr-x 1 user user 1453 Aug 11 2009 3.thumb.jpg -rwxr-xr-x 1 user user 14168 Aug 11 2009 4.jpg -rwxr-xr-x 1 user user 1464 Aug 11 2009 4.thumb.jpg [[email protected] ~]#
I have tried with permissions 777 and 755 which make no difference. For example in this folder, 1 and 2 may work whilst 3 & 4 don’t…
I ran a quick test to see what apache/php runs as and they do run as nobody, but this does not explain why some images work and some don’t with the exact same permissions.
EDIT 2: Problem solved. Can’t believe how stupid it was, some of the filenames which were called from a database had spaces at the end… Whoever made the original script didn’t run any sort of cleaning up before saving them.
ls -l /home/user/public_html/images/location/ and add the output to your question.
This is most likely a permission problem. PHP probably does not have read-access to the file.
After a lot of searches on the web, I found the solution myself. Hope it will be helpful for other people:
Ensure that the User and Group that are configured in your apache httpd.conf are the same like the apache group and user.
Perhaps, in a name of the file there is a whitespace symbol. It’s important.
I also had problems with getting the right result.
It turns out that it for me was a permission problem located on the folder.
You got to make sure that folders have the eXecute permission for the Apache-user.
To fix it I just did this on my linux-box:
chmod g+x csv_files