I'm trying to create a function which will run periodically to delete any old "user" folders which don't have an active user. This is the function I have:
function delete_temp_user_files() {
$dir = $_SERVER['DOCUMENT_ROOT'] . "/users/";
$dir_files = array();
$dir_files = scandir($dir);
foreach ($dir_files as $username) {
if ($username == "." || $username == "..") continue;
if (!user_exist($username)) {
$dir1 = $_SERVER['DOCUMENT_ROOT'] . "/users/" . $username;
if (!is_dir($dir1)) continue;
if (file_exists($dir1)) unlink($dir1);
}
}
}
But when it tries to delete the directory I get the error "Warning: unlink(/path/to/users/delete1/): Is a directory in /page.php on line..." I know the directory exists because I can see it in the directory, and it found it using scandir() anyway.
I use unlink to delete these same folders in other scripts and it works fine. The directories aren't empty so I can't user rmdir().
I'm not familiar with permissions or anything like that, is this some kind of issue with that? And do I need to worry about permissions if I only ever use PHP scripts to delete files and folders (like when the user clicks on the delete button which runs the script I wrote)?
UPDATE:
After scouring the web I finally found how to delete a directory and it's not easy lol Add this function into the previous function and it works!
function delete_dir($directory) {
foreach(glob("{$directory}/*") as $file)
{
if(is_dir($file)) {
delete_dir($file);
} else {
unlink($file);
}
}
rmdir($directory);
}