touch()

touch() — Sets access and modification time of file.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

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

Signature

touch(string $filename, ?int $mtime = null, ?int $atime = null): bool

Parameters

ParameterTypeWhat it takes
$filenamestringThe name of the file being touched.
$mtime?intThe touch time. If mtime is null, the current system time() is used.
$atime?intIf not null, the access time of the given filename is set to the value of atime. Otherwise, it is set to the value passed to the mtime parameter. If both are null, the current system time is used.

Return value

Returns true on success or false on failure.

Example

A runnable snippet:

if (touch($filename)) { echo $filename . ' modification time has been changed to present time'; } else { echo 'Sorry, could not change modification time of ' . $filename; }

Changelog

VersionChange
8.0.0mtime and atime are now nullable.

All PHP 8.5 built-in functions

Reference card built from the PHP manual page touch() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.