I'm creating a cron job that will autoflush the tmp directory everyday to make sure the tmp directory is not flooded with unwanted files.
However i want to delete all files and folders in the tmp directory except some files like .htaccess
i'm using the below code, but is giving an error
$filesToKeep = array(
'.htaccess'
// 'i.php',
// 'c.php'
);
$dir = '../tmp/';
$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file)
{
if (! in_array($file, $filesToKeep))
{
if ($file->isDir())
rmdir($file->getRealPath());
}
else
unlink($file->getRealPath());
}
Warning: rmdir(D:\Development(s)\Project(s)\blog\app\tmp\error_pages): Directory not empty
before this is used to run the below code which ran perfectly but also used to delete the .htaccess
file
$dir = '../tmp/';
$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file)
{
if ($file->isDir())
rmdir($file->getRealPath());
else
unlink($file->getRealPath());
}