touch

(PHP 4, PHP 5, PHP 7, PHP 8)

touchSetzt die Zugriffs- und Modifikationszeit einer Datei

Beschreibung

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

Versucht, die Zugriffs- und Modifikationszeit der im Parameter filename angegebenen Datei auf mtime zu setzen. Zu beachten ist, dass die Zugriffszeit unabhängig von der Anzahl der Parameter immer geändert wird.

Wenn die Datei nicht existiert, wird sie erzeugt.

Parameter-Liste

filename

Der Name der zu ändernden Datei.

mtime

Die Modifikationszeit. Wenn mtime null ist, wird die aktuelle Systemzeit (time()) verwendet.

atime

Wenn nicht null, wird die Zugriffszeit der angegebenen Datei auf atime gesetzt. Andernfalls wird sie auf den Wert des angegebenen Parameters mtime gesetzt. Wenn beide null sind, wird die aktuelle Systemzeit verwendet.

Rückgabewerte

Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben.

Changelog

Version Beschreibung
8.0.0 mtime und atime sind jetzt nullbar.

Beispiele

Beispiel #1 touch()-Beispiel

<?php
if (touch($filename)) {
echo
'Die Modifikationszeit von ' . $filename
. ' wurde auf die aktuelle Zeit gesetzt.';
} else {
echo
'Entschuldigung, die Änderung der Modifikationszeit von ' . $filename
. ' war nicht möglich.';
}
?>

Beispiel #2 Nutzung von touch() mit dem Parameter mtime

<?php
// Modifikationszeit (eine Stunde in der Vergangenheit)
$time = time() - 3600;

// Ändern der Datei
if (!touch('eine_datei.txt', $time)) {
echo
'Ein Fehler ist aufgetreten ...';
} else {
echo
'Änderung der Modifikationszeit war erfolgreich';
}
?>

Anmerkungen

Hinweis:

Beachten Sie, dass die zeitliche Auflösung bei verschiedenen Dateisystemen unterschiedlich sein kann.

add a note

User Contributed Notes 14 notes

up
16
anon
11 years ago
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' );
?>
up
12
Charles Belov
18 years ago
Update the access time without updating the modified time:

Unix command: touch -a filename

PHP: touch(filename, date('U', filemtime(filename)), time())
up
12
Jeff
16 years ago
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);
up
7
mrgrier at yahoo dot com
16 years ago
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.
up
7
ernst at cron-it dot de
15 years ago
To touch a file without being owner, it is much easier:

<?php
function touchFile($file) {
fclose(fopen($file, 'a'));
}
?>
up
3
chris dot dallaire at csquaredsystems dot com
14 years ago
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.
up
3
ddalex at gmail dot com
16 years ago
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.
up
1
info at archiwumrocka dot art dot pl
16 years ago
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($files1, 2);

foreach (
$files1 as $key => $val)
{
if (!
is_dir($val)) continue;
if (!
touch($val))
{
touch($val . "/plik.txt");
unlink($val . "/plik.txt");
}
}
?>
up
1
rf_public at yahoo dot co dot uk
19 years ago
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
up
1
feathern at yahoo dot com
22 years ago
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;
}
up
0
picek dot jaroslav at protonmail dot com
10 months ago
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
up
0
centurianii at yahoo dot co dot uk
7 years ago
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()!
up
-2
Glen
17 years ago
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}");
}
?>
up
-3
Radon8472
16 years ago
Important info:

touch() used on a directory always returns FALSE and prints "Permission denied" on NTFS and FAT Filesystem (tested on winXP).
To Top