I already read it so many questions and answers about it but I can\'t still solve my problem...
I\'m trying to create a function that deletes all the files with \"xm
For me the filename involved was appended with a querystring, which this function didn't like.
$path = 'path/to/my/file.js?v=2'
Solution was to chop that off first:
$path = preg_replace('/\?v=[\d]+$/', '', $path);
$fileTime = filemtime($path);
in my case it was not related to the path or filename. If filemtime(), fileatime() or filectime() don't work, try stat().
$filedate = date_create(date("Y-m-d", filectime($file)));
becomes
$stat = stat($directory.$file);
$filedate = date_create(date("Y-m-d", $stat['ctime']));
that worked for me.
Complete snippet for deleting files by number of days:
$directory = $_SERVER['DOCUMENT_ROOT'].'/directory/';
$files = array_slice(scandir($directory), 2);
foreach($files as $file)
{
$extension = substr($file, -3, 3);
if ($extension == 'jpg') // in case you only want specific files deleted
{
$stat = stat($directory.$file);
$filedate = date_create(date("Y-m-d", $stat['ctime']));
$today = date_create(date("Y-m-d"));
$days = date_diff($filedate, $today, true);
if ($days->days > 1)
{
unlink($directory.$file);
}
}
}
I think the problem is the realpath of the file. For example your script is working on './', your file is inside the directory './xml'. So better check if the file exists or not, before you get filemtime or unlink it:
function deleteOldFiles(){
if ($handle = opendir('./xml')) {
while (false !== ($file = readdir($handle))) {
if(preg_match("/^.*\.(xml|xsl)$/i", $file)){
$fpath = 'xml/'.$file;
if (file_exists($fpath)) {
$filelastmodified = filemtime($fpath);
if ( (time() - $filelastmodified ) > 24*3600){
unlink($fpath);
}
}
}
}
closedir($handle);
}
}
Shorter version for those who like short code:
// usage: deleteOldFiles("./xml", "xml,xsl", 24 * 3600)
function deleteOldFiles($dir, $patterns = "*", int $timeout = 3600) {
// $dir is directory, $patterns is file types e.g. "txt,xls", $timeout is max age
foreach (glob($dir."/*"."{{$patterns}}",GLOB_BRACE) as $f) {
if (is_writable($f) && filemtime($f) < (time() - $timeout))
unlink($f);
}
}