The code down below delete files inside the folder ” Images ” every 60 seconds, it works, but when the folder is empty it says: Warning: Invalid argument supplied for foreach()
How can that be fixed like if there is no files, say ” folder empty instead of that warning..
<?php
$expiretime=1;
$tmpFolder="Images/";
$fileTypes="*.*";
foreach (glob($tmpFolder . $fileTypes) as $Filename) {
// Read file creation time
$FileCreationTime = filectime($Filename);
// Calculate file age in seconds
$FileAge = time() - $FileCreationTime;
// Is the file older than the given time span?
if ($FileAge > ($expiretime * 60)){
// Now do something with the olders files...
echo "The file $Filename is older than $expiretime minutes\r\n";
//delete files:
unlink($Filename);
}
}
?>
Since glob() may not reliably return an empty array for an empty match (See “note” in Return section of the docs), you just need an
ifstatement protecting your loop, like so: