PHP | touch( ) function

The touch() function in PHP is an inbuilt function which is used setting the access and modification time of a specified file.
The filename of the file whose access and modification time has to be set is sent as a parameter along with the time to the touch() function and it returns True on success and False on failure. A file is created first if it doesn’t exist.

Syntax:

touch(filename, time, atime)

Parameters Used:
The touch() function in PHP accepts three parameters.



  1. filename : It is a mandatory parameter which specifies the filename of the file whose access and modification time have to be changed.
  2. time : It is an optional parameter which specifies the time.By default it takes the current system time.
  3. atime : It is an optional parameter which specifies the access time. By default it takes the current system time if no parameters are set.

Return Value:
It returns True on success and False on failure.

Errors And Exception

  1. The time resolution may differ from one file system to another therefore you may get unexpected results sometimes.
  2. The $time parameter in the touch() function has a future limit around 1000000 seconds.
  3. The touch() function used on a directory returns FALSE and prints “Permission denied” on NTFS and FAT Filesystem.

Examples:

Input : $file_pointer = "gfg.txt";
        if (touch($file_pointer)) 
        {
           echo ("$file_pointer modification time has been set to current system time.");
        } 
        else 
        {
           echo ("$file_pointer modification time cannot be changed.");
        }
Output :gfg.txt modification time has been set to current system time.

Input : $file_pointer = "gfg.txt";
        $time = time() - 18000;
        if (touch($file_pointer, $time)) 
        {
           echo ("$file_pointer modification time has been changed to 5 hours in the past.");
        } 
        else 
        {
           echo ("$file_pointer modification time cannot be changed.");
        }

Output : gfg.txt modification time has been changed to 5 hours in the past.

Below programs illustrate the touch() function.



Suppose there is a file named “gfg.txt”

Program 1

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$file_pointer = "gfg.txt";
// using touch() function to change the modification 
// time of a file to current system time
if (touch($file_pointer)) 
{
   echo ("$file_pointer modification time has been set to current system time.");
else 
{
   echo ("$file_pointer modification time cannot be changed.");
}
  
?>

chevron_right


Output:

gfg.txt modification time has been set to current system time.

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$file_pointer = "gfg.txt";
  
// setting touch time to 5 hours in the past
$time = time() - 18000;
  
// using touch() function to change the modification 
// time of a file to current system time
if (touch($file_pointer, $time)) 
{
    echo ("$file_pointer modification time has been changed to 5 hours in the past.");
 
else 
{
   echo ("$file_pointer modification time cannot be changed.");
}
  
?>

chevron_right


Output:

gfg.txt modification time has been changed to 5 hours in the past.

Reference:
http://php.net/manual/en/function.touch.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.