touch

(PHP 4, PHP 5)

touchSets access and modification time of file

Description

bool touch ( 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.

Examples

Example #1 touch() example

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

Example #2 touch() using the time parameter

<?php
// This is the touch time, we'll set it to one hour in the past.
$time time() - 3600;

// Touch the file
if (!touch('some_file.txt'$time)) {
    echo 
'Whoops, something went wrong...';
} else {
    echo 
'Touched file with success';
}
?>

Notes

Note:

Note that time resolution may differ from one file system to another.

Warning

Prior to PHP 5.3.0 it was not possible to change the modification time of a directory with this function under Windows.

Коментарии

Neat little script that will give you a list of all modified files in a certain folder after a certain date:

$filelist = Array();
$filelist = list_dir("d:\\my_folder");
for($i=0;$i<count($filelist);$i++){
    $test = Array();
    $test = explode("/",date("m/d/Y",filemtime($filelist[$i])));
//example of files that are later then
//06/17/2002
    if(($test[2] > 2001) && ($test[1] > 16) && ($test[0] > 5)){
        echo $filelist[$i]."\r\n";
    }
    clearstatcache();
}
function list_dir($dn){
    if($dn[strlen($dn)-1] != '\\') $dn.='\\';
    static $ra = array();
    $handle = opendir($dn);
    while($fn = readdir($handle)){
        if($fn == '.' || $fn == '..') continue;
        if(is_dir($dn.$fn)) list_dir($dn.$fn.'\\');
        else $ra[] = $dn.$fn;
    }
    closedir($handle);
    return $ra;
}
2002-08-13 18:31:07
http://php5.kiev.ua/manual/ru/function.touch.html
Here's a little workaround that allows the PHP user to touch a file it doesn't own:

<?php

    $target_file 
"/path/to/file/filename.txt"//system filepath to your file
   
$file_content implode("",file($target_file));
    @
unlink($target_file);
    if(
$savetofile fopen($target_file"w")) {
       
fputs($savetofile$file_content);
       
fclose($savetofile);
    }
   
$new_date strtotime("23 April 2005"); // set the required date timestamp here
   
touch($target_file,$new_date);
 
?>

Of course, PHP needs to have write access to the folder containing the file you want to touch, but that should be easy to arrange.
2005-05-12 06:42:33
http://php5.kiev.ua/manual/ru/function.touch.html
Note: the script to touch a file you don't own will change it's owner so ensure permissions are correct or you could lose access to it
2005-07-25 15:19:14
http://php5.kiev.ua/manual/ru/function.touch.html
If you're going to go around deleting (unlinking) files
that you don't own just in order to change the modification
time on the file, you darn well better chown() the file 
back to it's original ownership after you are done and
chmod() it back to it's correct permissions.  Otherwise 
you will almost certainly break things.  Additionally the
code 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 different
name 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.
2005-09-01 08:09:00
http://php5.kiev.ua/manual/ru/function.touch.html
Автор:
Update the access time without updating the modified time:

Unix command: touch -a filename

PHP: touch(filename, date('U', filemtime(filename)), time())
2006-07-18 21:10:14
http://php5.kiev.ua/manual/ru/function.touch.html
Автор:
In unix on the command-line, you can touch files you don't own - but like other comments on this page state - PHP's built in touch won't work.

I simple alternative (on unix):

<?php

   
function touch_it_good($filename)
    {
       
exec("touch {$filename}");
    }
?>
2007-10-18 16:01:58
http://php5.kiev.ua/manual/ru/function.touch.html
Автор:
I've been trying to set a filemtime into the future with touch() on PHP5.

It seems touch $time has a future limit around 1000000 seconds (11 days or so). Beyond this point it reverts to a previous $time.

It doesn't make much sense but I could save you hours of time.

$time = time()+1500000;
touch($cachedfile,$time);
2008-06-26 15:29:10
http://php5.kiev.ua/manual/ru/function.touch.html
Автор:
Important info:

touch() used on a directory always returns FALSE and prints "Permission denied" on NTFS and FAT Filesystem (tested on winXP).
2008-08-08 08:07:37
http://php5.kiev.ua/manual/ru/function.touch.html
Actually, Glen is right, PHP won't touch if it is not the current owner of the file, even if the directory and files are writeable by the PHP user.
2008-11-03 11:25:05
http://php5.kiev.ua/manual/ru/function.touch.html
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.
2008-11-29 12:29:55
http://php5.kiev.ua/manual/ru/function.touch.html
Only way to change modification date in catalogue is to create file in via touch() and dalete it with unlink():

<?php
$dir   
'temp';
$files1 scandir($dir);

$files1 array_slice($files12);

foreach (
$files1 as $key => $val
{
    if (!
is_dir($val)) continue;
    if (!
touch($val)) 
    {
       
touch($val "/plik.txt");
       
unlink($val "/plik.txt");
    }
}
?>
2008-12-01 15:59:50
http://php5.kiev.ua/manual/ru/function.touch.html
To touch a file without being owner, it is much easier:

<?php
function touchFile($file) {
 
fclose(fopen($file'a'));
}
?>
2009-09-29 09:38:30
http://php5.kiev.ua/manual/ru/function.touch.html
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.
2010-04-09 10:41:54
http://php5.kiev.ua/manual/ru/function.touch.html
I has passed a small test to check which function is faster to create a new file. 

file_put_contents vs touch

<?php
for($i 0$i 100$i++)
{
     
file_put_contents('dir/file'.$i'');
}
?>
Average time: 0,1145s

<?php
for($i 0$i 100$i++)
{
     
touch('dir/file'.$i);
}
?>
Average time: 0,2322s

So, file_put_contents is faster than touch, about two times.
2010-10-08 17:11:13
http://php5.kiev.ua/manual/ru/function.touch.html
Автор:
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":

<?php
    touch
'somefile' );
?>

But this will:

<?php
    touch
__DIR__ '/somefile' );
?>
2013-11-10 18:38:56
http://php5.kiev.ua/manual/ru/function.touch.html
An earlier comment referenced a code snippet that showed file_put_contents() was faster the touch for creating files. I re-ran the same tests on PHP 5.5.9 and this seems to no longer be the case.

<?php
$startTime 
microtime(true);
for(
$i 0$i 100000$i++)
{
   
file_put_contents('dir/file'.$i'');
   
unlink('dir/file'.$i);
}
echo 
"Time: ".(microtime(true)-$startTime)."\n"// Time: 2.6902809143066

$startTime microtime(true);
for(
$i 0$i 100000$i++)
{
   
touch('dir/file'.$i);
   
unlink('dir/file'.$i);
}
echo 
"Time: ".(microtime(true)-$startTime)."\n"// Time: 2.3343770503998
?>
2014-12-14 10:41:04
http://php5.kiev.ua/manual/ru/function.touch.html
A better explanation:

For file $file and UNIX time stored in vars $access and $modified

- change only access time
\touch($file, \filemtime($file), $access);

- change only modified time
\touch($file, $modified, \fileatime($file));

- change both access and modified time
\touch($file, $modified, $access);

Seeing the results:

//use a session cookie stored in a custom folder
$file = '/var/www/test_com/session/sess_qfn587cudfpgsijm1bs4d81s75';
echo 'stats for sess_qfn587cudfpgsijm1bs4d81s75<br/>';
\clearstatcache();
echo 'access: '.\date("Y-m-d H:i:s", \fileatime($file)).'<br/>';
echo 'modified: '.\date("Y-m-d H:i:s", \filemtime($file)).'<br/>';
echo 'change access to now, modified +1 hour<br/>';
\touch($x, \filemtime($file)+3600, time());
\clearstatcache();
echo 'access: '.\date("Y-m-d H:i:s", \fileatime($file)).'<br/>';
echo 'modified: '.\date("Y-m-d H:i:s", \filemtime($file)).'<br/>';

Notice the double call to clearstatcache()!
2017-08-17 11:01:12
http://php5.kiev.ua/manual/ru/function.touch.html
I have found out that setting a negative *mtime* deletes the file. The following code always deletes the file at $path while $touch returns true.

<?php
$path 
'/folder/file';
$timestamp = -1;

$touch touch($path$timestamp);
?>

Running PHP 7.4.5
2024-02-12 00:32:11
http://php5.kiev.ua/manual/ru/function.touch.html

    Поддержать сайт на родительском проекте КГБ