Tuesday 2 June 2015

Modify file date on server

Use this snippet to modify the access and modification times of a file on the server 
<?php
// $ptf: absolute path to the file
// $str_date: the date to set it to i.e. (23 November 2008 22:30:10)
function set_file_mod_time($ptf, $str_date)
{
 return touch($ptf, strtotime($str_date));
}
?>
 

Usage

This sets the access and modification times of a file on the server.
Returns true or false.
So call it like:

if (!set_file_mod_time('/home/user/public_html/file.txt', '23 November 2008 22:30:10'))
{
echo 'Something went wrong';
}
else
{
echo 'Modified time changed!';
}

Of course the pitfall is that the process must own the file. On a plain environment 
it should be nobody or www the file owner.
 

0 comments:

Post a Comment