Deleting a directory while deleting a user in CMS

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!

you have to delete all the files in the directory before you can delete the directory. Give this a shot
[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 (is_dir($dir))
$dir_handle = opendir($dir);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != “.” && $file != “…”) {
if (!is_dir($dir."/".$file))
unlink($dir."/".$file);
else
delete_directory($dir.’/’.$file);
}
}
closedir($dir_handle);
rmdir($dir);
return true;
}

removeDir();

$deletemain = “…/images/girls_lg/$subuser.jpg”;

$deletethumb = “…/images/girls_sm/$subuser.jpg”;

unlink($deletemain);

unlink($deletethumb);
header("Location: ".$session->referrer);
}
}
[/php]

Thanks for looking over the code for me. I switched my code out with the information you gave me. Unfortunately, that directory still isn’t being deleted. Any idea what the problem might be?

Thank you again.

hmm maybe it might be because you are deleteing the user from the database first then when you create the variable $dir, it is looking for subuser, which has already been deleted. try moving the delete user from database , to after delete directory.

I’m getting a Missing Argument error for the first line of the function. That seems to be the only error I’m getting, as far as I can tell. It isn’t deleting the files within the folder either.

“Warning: Missing argument 1 for removeDir(), called in D:\Hosting\5876595\html\test\ADMIN\admin\adminprocess.php on line 375 and defined in D:\Hosting\5876595\html\test\ADMIN\admin\adminprocess.php on line 339”

the line it references is:
function removeDir($dir) {

Maybe this can help?

I thought of this, too. I moved the database deletion, and am still getting this error. I think part of the problem was that there is a folder within a folder. I’ve created a file to JUST do this task, without all the other stuff going on:

[php]<?php
$dir = ‘…/images/girls_lg/Misty/thumbs/’;
$maindir = ‘…/images/girls_lg/Misty/’; // example.

function removeDir($dir) {
if (is_dir($dir))
$dir_handle = opendir($dir);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != “.” && $file != “…”) {
if (!is_dir($dir."/".$file))
unlink($dir."/".$file);
else
delete_directory($dir.’/’.$file);
}
}
closedir($dir_handle);
rmdir($dir);
return true;
}
function removeMainDir($maindir) {
if (is_dir($maindir))
$maindir_handle = opendir($maindir);
if (!$maindir_handle)
return false;
while($file2 = readdir($maindir_handle)) {
if ($file2 != “.” && $file2 != “…”) {
if (!is_dir($maindir."/".$file2))
unlink($maindir."/".$file2);
else
delete_directory($maindir.’/’.$file2);
}
}
closedir($dir_handle);
rmdir($dir);
return true;
}

removeDir();
removeMainDir();
?>[/php]

And here’s the error I’m getting:
Warning: Missing argument 1 for removeDir(), called in D:\Hosting\5876595\html\test\ADMIN\admin\test.php on line 41 and defined in D:\Hosting\5876595\html\test\ADMIN\admin\test.php on line 5

Warning: Missing argument 1 for removeMainDir(), called in D:\Hosting\5876595\html\test\ADMIN\admin\test.php on line 42 and defined in D:\Hosting\5876595\html\test\ADMIN\admin\test.php on line 22

I figure starting with the basics may help. Thanks again!

you forgot to add which directory you are removing
[php]
removeDir();
removeMainDir();
?>[/php]

should be:

[php]removeDir($dir);
removeMainDir($maindir);
?>[/php]

Thank you! Didn’t quite work after that, but I was able to fix the other problems once I had that fixed. Everything’s working the way I want, now.

Thank you again!

In case anyone wants to see it in the future, here’s the final 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{
  $username = $_POST['deluser'];

$dir = ‘…/images/girls_lg/’.$username.’/thumbs/’;
$maindir = ‘…/images/girls_lg/’.$username.’/’; // example.

function removeDir($dir) {
if (is_dir($dir))
$dir_handle = opendir($dir);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle)) {
if ($file != “.” && $file != “…”) {
if (!is_dir($dir."/".$file))
unlink($dir."/".$file);
else
delete_directory($dir.’/’.$file);
}
}
closedir($dir_handle);
rmdir($dir);
return true;
}
function removeMainDir($maindir) {
if (is_dir($maindir))
$maindir_handle = opendir($maindir);
if (!$maindir_handle)
return false;
while($file2 = readdir($maindir_handle)) {
if ($file2 != “.” && $file2 != “…”) {
if (!is_dir($maindir."/".$file2))
unlink($maindir."/".$file2);
else
delete_directory($maindir.’/’.$file2);
}
}
closedir($maindir_handle);
rmdir($maindir);
return true;
}

removeDir($dir);
removeMainDir($maindir);

$deletemain = “…/images/girls_lg/$subuser.jpg”;

$deletethumb = “…/images/girls_sm/$subuser.jpg”;
if(file_exists($deletemain)){
unlink($deletemain);}else{}

if(file_exists($deletethumb)){

unlink($deletethumb);}else{}
$q = “DELETE FROM “.TBL_USERS.” WHERE username = ‘$subuser’”;
$database->query($q);

     header("Location: ".$session->referrer);
  }

}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service