English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Examples of PHP touch() Function

PHP Filesystem Reference Manual

The touch() function can set the access and modification time of the specified file, returns true on success, and false on failure.

Syntax

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

This function can attempt to set the access and modification time of the file named in the filename parameter to the given value in a timely manner. Note that the access time will always be modified regardless of the number of parameters.

Example1

<?php
   $filename = "/PhpProject/sample.txt";
   if(touch($filename)) {
      echo $filename . " modification time has been changed to the current time";
   } else {
      echo "Sorry, unable to change the modification time of " . $filename . "";
   }
?>

Output result

/PhpProject/The modification time of sample.txt has been changed to the current time

Example2

<?php
   $time = time() - 3600;
   if (!touch("/PhpProject/sample.txt", $time)) {
      echo "Oops, something went wrong...";
   } else {
      echo "File accessed successfully";
   }
?>

Output result

File accessed successfully

PHP Filesystem Reference Manual