If you're going to go around deleting (unlinking) filesthat you don't own just in order to change the modificationtime on the file, you darn well better chown() the file back to it's original ownership after you are done andchmod() it back to it's correct permissions. Otherwise you will almost certainly break things. Additionally thecode listed for touch()ing a file you don't own should set the file creation time back to it's original time if what is wanted is to just change the modification time.Also, the code listed will break things if there is an i/o error such as disk full or too many files in the directory.Here's how the code SHOULD be written:

Create the new file FIRST, rather than last, with a differentname such as $file.tmp.Read the ownership, permissions, and creation time of the old file.Set permissions and creation time of the new file the same as the old.Rename the new file to the name of the old.chown() the new file to the user that owned the file it's replacing.

Please be careful adding to the documentation if you've never taken programming 101.

Note that when PHP is called by f.e. apache or nginx instead of directly from the command line, touch() will not prefix the location of the invoking script, so the supplied filename must contain an absolute path.

With script started from /home/user/www, this will not touch "/home/user/www/somefile":

At least on Linux, touch will not change the time on a symlink itself, but on the file/directory it points to. The only way to work around this is to unlink the symlink, then recreate it.

It took a bit of searching to discover this. The OS itself provides no way to do it. Many people wondered why anyone would want to do this. I use symlinks inside a web tree to point to files outside the web tree. After a certain length of time has passed, I want the symlinks to die, so the files cannot be successfully hotlinked.

I needed to use this to touch the /etc/cron.d directory when I updated some files in there. I know the docs say this isn't necessary, but I'm finding that i need to do it in order form my changes to be picked up quickly.

I ran into the permissions error as well and I found that using chmod 777 /etc/cron.d does the trick.

So, you should be able to use the PHP touch function on a directory that has open write access.

Of course, this isn't the most secure approach, but in our application it's not a big deal for that folder to not be super secure.