Jump to content

Recommended Posts

Hello there,

 

I've been trying to delete a folder for a particular user. I found that you can't delete the folder unless it is empty. So i have been trying to look for ways to loop through deleting the content then removing the folder.

 

so far i've got the following... (and it doesnt work so im stuck!)

 

function deltree( $f ){

    if( is_dir( $f ) ){
        foreach( scandir( $f ) as $item ){
            if( !strcmp( $item, '.' ) || !strcmp( $item, '..' ) ) 
                continue;        
            deltree( $f . "/" . $item );
        }    
        rmdir( $f );
    }
    else{
        unlink( $f );
    }
} 

Link to comment
https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/
Share on other sites

try this

 

<?php
// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone

function SureRemoveDir($dir, $DeleteMe) {
    if(!$dh = @opendir($dir)) return;
    while (false !== ($obj = readdir($dh))) {
        if($obj=='.' || $obj=='..') continue;
        if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
    }

    closedir($dh);
    if ($DeleteMe){
        @rmdir($dir);
    }
}

//SureRemoveDir('EmptyMe', false);
//SureRemoveDir('RemoveMe', true);

?>

				$dir = "/testfolder/test/";
				echo "$dir";
// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone

function SureRemoveDir($dir, $DeleteMe) {
   if(!$dh = @opendir($dir)) return;
   while (false !== ($obj = readdir($dh))) {
       if($obj=='.' || $obj=='..') continue;
       if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
   }

   closedir($dh);
   if ($DeleteMe){
       @rmdir($dir);
   }
}

SureRemoveDir('EmptyMe', false);
SureRemoveDir('RemoveMe', true);

 

is that right?

 

I think the permissions are all set, theres no errors coming up about permission denied.

				$dir = "/testfolder/test";
				echo "$dir";
// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone

function SureRemoveDir($dir, $DeleteMe) {
    if(!$dh = @opendir($dir)) return;
    while (false !== ($obj = readdir($dh))) {
        if($obj=='.' || $obj=='..') continue;
        if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
    }

    closedir($dh);
    if ($DeleteMe){
        @rmdir($dir);
    }
}

 

got that now? still doesnt work

<?php

$dir = "/testfolder/test"; //Remove the trailing /
if(!file_exists($dir)) echo "Folder '$dir' doesn't exist";
echo "$dir";

SureRemoveDir($dir, true);

// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone

function SureRemoveDir($dir, $DeleteMe) {
   if(!$dh = @opendir($dir)) return;
   while (false !== ($obj = readdir($dh))) {
       if($obj=='.' || $obj=='..') continue;
       if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
   }

   closedir($dh);
   if ($DeleteMe){
       @rmdir($dir);
   }
}

?>

Its working, had to make a couple of modifications but that code and help you both gave me was a great help. i really appreciate it!!

 

				$dir = "testfolder/test/";
				//echo "$dir";

//$dir = "/testfolder/test"; //Remove the trailing /
if(!file_exists($dir)) echo "Folder '$dir' doesn't exist";
echo "$dir";


// $dir = the target directory
// $DeleteMe = if true delete also $dir, if false leave it alone

function SureRemoveDir($dir, $DeleteMe) {
   if(!$dh = @opendir($dir)) return;
   while (false !== ($obj = readdir($dh))) {
       if($obj=='.' || $obj=='..') continue;
       if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true);
   }

   closedir($dh);
   if ($DeleteMe){
       @rmdir($dir);
   }
}
SureRemoveDir($dir, true);

 

I had to put SureRemoveDir($dir, true); after the code as it was trying to call the function before i had defined it.

 

Also, $dir = "testfolder/test/";... i had to take off the / from before testfolder (i previously had $dir = "/testfolder/test/"; ) and it was saying the folder did not exist. Once i removed the beginning slash it worked a treat!

 

Thanks again lads!!

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.