PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

umask> <tmpfile
Last updated: Fri, 20 Jun 2008

view this page in

touch

(PHP 4, PHP 5)

touch — Modifie la date de modification et de dernier accès d'un fichier

Description

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

Tente de forcer la date de modification du fichier désigné par le paramètre filename à la date de spécifiée par le paramètre time . Notez bien que la date de dernier accès est modifiée, quelque soit le nombre de paramètres.

Si le fichier n'existe pas, PHP tentera de le créer.

Liste de paramètres

filename

Le nom du fichier à créer.

time

La date de création. Si time est omis, c'est l'heure courante qui est utilisée.

atime

Si le troisième paramètre atime est fourni, il est utilisé comme date de dernier accès.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.

Exemples

Exemple #1 Exemple avec touch()

<?php
if (touch($FileName)) {
    echo 
"La date de modification de $FileName a
    été fixée à maintenant"
;
} else {
    echo 
"Désolé, il est impossible de changer 
    la date de modification de $FileName"
;
}
?>

Notes

Avertissement

Il n'est pour le moment pas possible de changer la date de modification d'un dossier avec cette fonction sous Windows.



umask> <tmpfile
Last updated: Fri, 20 Jun 2008
 
add a note add a note User Contributed Notes
touch
Jeff
26-Jun-2008 12:29
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);
Glen
18-Oct-2007 01:01
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}");
    }
?>
ilrazziatore85 AT yahoo DOT it
21-May-2007 08:10
Feathern wrote a little script for fetching files from a directory after a certain date.
However the if statement (line 8) should be:

if(($test[2] > 2002) || (($test[2] = 2002) && ($test[0] > 6)) || (($test[2] = 2002) && ($test[0] = 6) && ($test[1] > 17))){
        echo $filelist[$i]."\r\n";
}

Otherwise the script won't fetch lots of files it should.

(In the example given, it should fetch all the files created after 06/17/2002, but the original script would miss files created 03/18/2003 or 11/01/2004)
seocab at rit dot edu
31-Mar-2007 09:09
The script for modifying the access time without modifying the modified time is overly complicated:

<? touch($filename, date('U', filemtime($filename)), time()); ?>

Since filemtime returns a UNIX timestamp, there is no need to call date('U') so the script could be simplified to:

<? touch($filename,filemtime($filename),time()); ?>
Charles Belov
18-Jul-2006 06:10
Update the access time without updating the modified time:

Unix command: touch -a filename

PHP: touch(filename, date('U', filemtime(filename)), time())
spam at webmastersguide dot com
01-Sep-2005 05:09
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.
rf_public at yahoo dot co dot uk
25-Jul-2005 12:19
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
guy at forster design dot com
12-May-2005 03:42
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.
feathern at yahoo dot com
13-Aug-2002 03:31
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;
}
emilebosch at hotmail dot com
06-Oct-2001 03:41
To spare you ppl couple of hours of valuable time, you can only TOUCH a file that you own! Usually PHP is *nobody*
Warm regards,
Emile Bosch
master at dreamphp dot com
15-May-2001 11:23
$filename = "test.dat";
if (!file_exists($filename)) {
  touch($filename); // Create blank file
  chmod($filename,0666);
}

umask> <tmpfile
Last updated: Fri, 20 Jun 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites