KP Solutions

Solutions for Day to Day Technical Problems

is_file and file_exists fail with symbolic links


Recently, I came across an issue where is_file and file_exists would fail in case of file with symbolic links.

Let’s say I have a file at /home/kpsolu2/personal/images/rome.jpg and I do a symbolic link from another user /home/kpsolu3/public_html/images/rome.jpg as follows:

ln -s  /home/kpsolu2/personal/images/rome.jpg /home/kpsolu3/public_html/images/rome.jpg

Now, since this symbolic link is from one user’s account to another user’s account, you need to give proper permissions. I don’t advise this architecture, but there were reasons for which I had to do it this way. So let’s continue. I added kpsolu2 usergroup to user kpsolu3 using following command

usermod -a -G kpsolu2 kpsolu3

Now, I was able to do

ls -l /home/kpsolu3/public_html/images/rome.jpg

and get valid listing. But, in php, if i do

if ( is_file (‘/home/kpsolu3/public_html/images/rome.jpg’) )

I always get that file is not valid. So luckily, it struck in my mind, let me check my server settings for ‘mod_rewrite’ for the account ‘kpsolu3’. I found the reason why this issue occured.

 Options +SymLinksIfOwnerMatch

I had above setting, which forced web server that serve files only if owners matched but the owner was ‘kpsolu2’ and not ‘kpsolu3’. Hence to patch the things, I changed the setting in httpd.conf file from above to below:

Options +FollowSymlinks

Restarted the server and the code then started to work as is. I know this brings in complications and security issues, but the situation demanded it and I had to fix it. So there you go.

If this post helped you in any way or you have any questions then let me know via Comments below.