I’ve created a CMS (with a lot of help from the people on this board!), and now am trying to set up the following:
When a new user is created, a folder for their images and the thumbs of those images is made. I need to make it so that when a user is DELETED, the folder, images, and thumbs are all deleted as well.
Here’s my script so far. The script below shows the deletion of two jpg images located elsewhere meant as an avatar and profile picture. That part is working fine, as is the deletion of the user from the MySQL database. But I can’t get the rmdir() and the functions surrounding it to work!!!
THE FORM
<form action="adminprocess.php" method="POST">
Username:<br>
<input type="text" name="deluser" maxlength="30" value="<? echo $form->value("deluser"); ?>">
<input type="hidden" name="subdeluser" value="1">
<input name="submit" type="submit" value="Delete User">
</form>
The process page, just this portion of the code:
[php] /**
* procDeleteUser - If the submitted username is correct,
* the user is deleted from the database.
/
function procDeleteUser(){
global $session, $database, $form;
/ Username error checking */
$subuser = $this->checkUsername(“deluser”);
/* Errors exist, have user correct them */
if($form->num_errors > 0){
$_SESSION['value_array'] = $_POST;
$_SESSION['error_array'] = $form->getErrorArray();
header("Location: ".$session->referrer);
}
/* Delete user from database */
else{
$q = "DELETE FROM ".TBL_USERS." WHERE username = '$subuser'";
$database->query($q);
$dir = ‘…/images/girls_lg/$subuser/’; // example.
function removeDir($dir)
{
if (!file_exists($dir)) return true;
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $item)
{
if ($item == ‘.’ || $item == ‘…’) continue;
if (!removeDir($dir . “/” . $item))
{
if (!removeDir($dir . “/” . $item)) return false;
};
}
return rmdir($dir);
};
removeDir();
$deletemain = “…/images/girls_lg/$subuser.jpg”;
$deletethumb = “…/images/girls_sm/$subuser.jpg”;
unlink($deletemain);
unlink($deletethumb);
header("Location: ".$session->referrer);
}
}[/php]
Thanks ahead of time!