PHP Function Reference

PHP touch() Function



The PHP touch() function attempts to set the access and modification times of the specified file to the value specified by time. Please note that, If the file does not exist, it will be created.

Syntax

touch(filename, time, atime)

Parameters

filename Required. Specify the name of the file being touched.
time Optional. Specify the touch time. If time is not supplied, the current system time is used.
atime Optional. 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 Value

Returns true on success or false on failure.

Example:

Lets assume that we have a file called test.txt in the current working directory. The example below demonstrates on using this function to set the access and modification times of this file.

<?php
$file = 'test.txt';

//touching the $file
if(touch($file)){
  echo "$file modification time is changed to present time.\n";
} else {
  echo "Sorry, could not change modification time of $file.\n";
}
?>

The output of the above code will be:

test.txt modification time is changed to present time.

Example: using time parameter

The time parameter can be used to set the access and modification times of the specified file to specified time. Consider the example below:

<?php
//a time one hour in the past
$time = time() - 3600;

$file = 'test.txt';

//touching the $file and setting the 
//touch time one hour in the past 
if(touch($file, $time)){
  echo "Touched file with success.\n";
} else {
  echo "Something went wrong.\n";
}
?>

The output of the above code will be:

Touched file with success.

❮ PHP Filesystem Reference