Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would file_put_contents() succeed but touch() fail?

I'm running a script that makes some changes to the contents of a file then resets its modification time to what it was before. Intermittently, I'll find the following errors in my log:

touch() [function.touch]: Utime
failed: Operation not permitted

This on the line immediately after a file_put_contents() call appears to have changed the contents of the file I tried to touch(). There are no errors associated with the file_put_contents() line.

Has anyone had this happen? Can anyone figure out what set of permissions would allow me to write a file but not change its modification time? I'm doing this on Linux.

like image 214
Jeremy DeGroot Avatar asked Nov 04 '09 21:11

Jeremy DeGroot


1 Answers

This is a bug with PHP's touch command. Even if you have write permission to the file, it fails if PHP isn't also the "owner".

If you're using Apache and Linux, use this command on your server's console to make PHP the file's owner:

sudo chown www-data:www-data /YourPATH/YourFILE

Better still, update the entire folder containing files you want PHP to control:

sudo chown -R www-data:www-data /YourPATH/YourFOLDER

Side Note: Because PHP can write to the file, that means it must have user or group write permission. Since that's the case, touch should not behave this way. It seems like a bug.

like image 66
gavanon Avatar answered Oct 26 '22 07:10

gavanon