touch

(PHP 4, PHP 5, PHP 7)

touch — Sets access and modification time of file

Description

booltouch
( string$filename
[, int$time = time()
[, int$atime
]] )

Attempts to set the access and modification times of the file named in the
filename parameter to the value given in
time.
Note that the access time is always modified, regardless of the number
of parameters.

If the file does not exist, it will be created.

Parameters

filename

The name of the file being touched.

time

The touch time. If time is not supplied,
the current system time is used.

atime

If present, the access time of the given filename is set to
the value of atime. Otherwise, it is set to
the value passed to the time parameter.
If neither are present, the current system time is used.

Return Values

Returns TRUE on success or FALSE on failure.

Changelog

Version

Description

5.3.0

It became possible to change the modification time of a directory
under Windows.

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":

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.

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.