Page 1 of 1

Very strange problem with symbolic links

Posted: Wed Nov 12, 2008 3:46 am
by fudge
Hi everyone,

i've been doing php programming for many years now and now i met a strange behavior which it seems i cannot solve alone.

The situation is the following:

The php creates a directory which is a copy of another directory, but we're using symbolic links to point to the original files, instead of copying all the files physically.

So a file looks like this:
lrwxrwxrwx 1 www-data www-data 68 2008-11-11 17:54 info.inc.html -> /var/www/hotsys/public_html/customer/templates/default/info.inc.html

Now what the script is trying to do to allow the user to edit those files via a simple text box. So when a user press the EDIT on this file, the original file's contents coming up (which the symbolic link points to) in the text box, allowing the user to edit the contents. When the user press SAVE, we delete the symbolic link, and creates a new file with the same name and write the content into the new file. Simple, isn't it?

Code: Select all

unlink($filename); 
$f=fopen($filename,"w")or die("$filename ".$message['file_not_open']);
fwrite($f,$savenew);
fclose($f);
What i get is this:
Warning: fopen(/var/www/hotsys/public_html/customer/templates/custom/fudge/info.inc.html) [function.fopen]: failed to open stream: Permission denied in /var/www/hotsys/public_html/operator/skins.php on line 626
/var/www/hotsys/public_html/customer/templates/custom/fudge/info.inc.html File can not be opened
After some debugging i have found out the following:
- the unlink was successful, the symbolic link got deleted successfully
- somehow the php thinks that the file is still there, so cannot create the new file. But after a few reloads (running the same script again), it works!!! Sometimes it is happening after 5 seconds, sometimes after 30 seconds
- if i try to create the file from within ssh, after the unlink, it works without problems, even if i try with the www-data user, so only php thinks that the file is still there

OK i thought i try a different way and after the unlink i tried to create the file with an other name, it worked without a problem. Then i renamed it to the name which was identical to the symbolic link name, and it worked. I thought i solved the problem but i was wrong.

When i tried to edit the same file again, it showed the content of the original file, not the one i saved! I opened the file within ssh, and the content was updated, so the save was successful. But when i edit the file with the php script, it thinks that the file's content haven't changed, because i saved the file with a different name!!!! And now what, after some reloads, it finally gives me the new file's content.

I seems that php caches the dirlist, and the symbolic links content too!!!!

How is this possible?

AND all of these are working perfectly with normal files, so if i edit a normal file, not a symbolic link, then it gets saved successfully and gives me the changed content when i edit again. All the problems comes up when symbolic links are involved.

I've never seen something like this and i tried to find the solution yesterday but after 10 hours of trying i though i need some help from others...

Thanks!

Re: Very strange problem with symbolic links

Posted: Wed Nov 12, 2008 3:53 am
by VladSun

Re: Very strange problem with symbolic links

Posted: Wed Nov 12, 2008 4:14 am
by fudge
Thanks, but clearstatcache didn't solve the issue, i have tried it.

AND as it said "unlink() clears the cache automatically" so it's not related with that.

FURTHERMORE,

the script is working PERFECTLY on our test server:
PHP Version 4.4.9

but fails on our live server:
PHP Version 5.2.0-8+etch7