0

I am trying to delete a folder using this script :

function Delete($path)
{
    if (is_dir($path) === true)
    {
        $files = array_diff(scandir($path), array('.', '..'));

        foreach ($files as $file)
        {
                Delete(realpath($path) . '/' . $file);
        }

        return rmdir($path);
    }

    else if (is_file($path) === true)
    {
        return unlink($path);
    }

    return false;
}

Delete('tmp');

It works in my Xampp server, but not on my webserver. I have changed the permissions of the folder and of the file it contains to 0777. So it should be writable(or in this case erasable) but nothing happens. I have even tryied giving the absolute path of the folder as the parameter of the function, but still nothing.Any ideas?

Community
  • 1
  • 1
Vidi
  • 205
  • 1
  • 7
  • 12
  • 1
    Any error messages? You should be getting some. – Pekka Sep 02 '10 at 21:29
  • Try adding at the top `ini_set('display_errors', 1); error_reporting(E_ALL);` and see if any errors decide to show up. – Jim Sep 02 '10 at 22:04
  • No. Still nothing. Just a few notice messages that I was expecting. But they are not related. – Vidi Sep 02 '10 at 22:08

6 Answers6

1

Use this :

function delTree($dir)
{
 $files = glob( $dir . '*', GLOB_MARK );
 foreach( $files as $file 
 {
  if( is_dir( $file ) )
   delTree( $file );
  else
   @unlink( $file );
 }

 if( is_dir($dir) ) rmdir( $dir );
};
DJafari
  • 12,955
  • 8
  • 43
  • 65
0

You can Try this code

<?php
$files = glob('application/*'); foreach($files as $file){  if(is_file($file)) unlink($file);  }
?>

Or,

   function viewDir($path) {
    return is_file($path) ?
            @unlink($path) :
            array_map(__FUNCTION__, glob($path.'/*')) == @rmdir($path);
}
$dir=$_SERVER["DOCUMENT_ROOT"]."/xxxx/xxxx";
echo $dir;
viewDir($dir);
Inspire Shahin
  • 414
  • 2
  • 8
  • 24
0

Does it return false? Or it returns true but doesn't actually remove?

Normally I'd just guess it's a permissions problem.

Try making a folder with mkdir from PHP so PHP is the owner (so to speak) and try to remove that with your function.

If it works, it's a permissions/owner issue.

Francisc
  • 77,430
  • 63
  • 180
  • 276
  • I have tried this: if(mkdir("tmp", 0777)) echo('works'); else echo('nope'); if(Delete('tmp')) echo('yes'); else echo('no'); But nothing happens. It's not making the folder. And it's not showing anything. – Vidi Sep 02 '10 at 22:05
  • Are you sure your host allows those functions? – Jim Sep 02 '10 at 22:13
  • Yes, I just tried making a new php file and making a folder using the new php file. It worked. Interesting enough, altough I used mkdir("tmp",0777) it created a tmp folder with 0755 persmissions which are the default. Am I using the mkdir function wrong?Should the permissions value be in quotes? – Vidi Sep 02 '10 at 22:24
0

May be some files are opened using php like fopen and that time it will not delete the folder or directory. I have faced the same issue when I tried to delete a file/folder

0

Try something like this.

<?php
function delete_directory($target) {
         if (is_dir($target))
           $dir_handle = opendir($target);
     if (!$dir_handle)
          return false;
     while($file = readdir($dir_handle)) {
           if ($file != "." && $file != "..") {
                if (!is_dir($dirname."/".$file))
                     unlink($dirname."/".$file);
                else
                     delete_directory($target.'/'.$file);
           }
     }
     closedir($dir_handle);
     rmdir($target);
     return true;
}

?>

Hope this helps.

Ashman Malik
  • 267
  • 3
  • 15
0

Try this line of code to delete a folder or folder files I hope this will help you

function deleteAll($str) {
        if (is_file($str)) {
            return unlink($str);
        }
        elseif (is_dir($str)) {
            $scan = glob(rtrim($str,'/').'/*');
            foreach($scan as $index=>$path) {
                $this->deleteAll($path);
            }            
            return @rmdir($str);
        }
    }
Adeel Ahmed Baloch
  • 672
  • 2
  • 7
  • 15