I have a folder called images, within the folders of the users like:
Images
- user 1
- user 2
- etc..
When i'm deleting a user from the database, i would like to also remove all the files and the folder of that given user. all this is inside a class with functions, but when i'm execute the function my whole folder images get deleted...
I already checked if the correct user is selected, if the path is allright. while i'm testing it on a test.php file it works well but inside my function it get broke.
if clicked te delete button it go to this function:
$user = new user();
$id = $_db->mysqli->real_escape_string($_POST['id']);
$query = "SELECT * FROM users WHERE id = '" .$id."'";
$result = $_db->mysqli->query($query);
$userNumb = $result->num_rows;
$finalUserNumb = $userNumb;
if ($finalUserNumb > 0) {
$user->deleteUser($id);
}
Get the user info by the given id
public function userSelsectByID($selector, $id)
{
$query = "SELECT " .$selector. " FROM users WHERE id ='" .$id."'";
$result = $this->mysqli->query($query);
$userInfo = $result->fetch_assoc();
$itemResult = $userInfo[$selector];
return $itemResult;
}
delete files function
public function delete_files($target)
{
if(is_dir($target)){
$files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned
foreach( $files as $file ){
$this->delete_files( $file );
echo "Deleted ".$file." succesfull...</br>";
}
rmdir( $target );
} elseif(is_file($target)) {
unlink( $target );
}
}
delete the user function
public function deleteUser($id)
{
$user = "DELETE FROM users WHERE id = '$id'";
$userResult = $this->mysqli->query($user);
$uren = "DELETE FROM uren WHERE user_id = '$id'";
$urenResult = $this->mysqli->query($uren);
$cookieLogin = "DELETE FROM cookieLogin WHERE user_id = '$id'";
$cookieLoginResult = $this->mysqli->query($cookieLogin);
$gebruikersnaam = str_replace(' ', '_', $this->userSelsectByID('gebruikersnaam', $id));
$this->delete_files('/sites/domain.nl/www/admin/images/'.$gebruikersnaam);
}
I want that the folder with the username is deleted, but in fact the whole images folder gets deleted... i have tried a lot but nothing works :(
Someone that can help me?