0

Please answer this question and help me!!

I have found a dozen of code snippets about deleting a folder with its all files and sub folders in PHP. But I am facing difficulties to apply them.

This is an example code :

function rrmdir($path) {
 // Open the source directory to read in files
    $i = new DirectoryIterator($path);
    foreach($i as $f) {
        if($f->isFile()) {
            unlink($f->getRealPath());
        } else if(!$f->isDot() && $f->isDir()) {
            rrmdir($f->getRealPath());
        }
    }
    rmdir($path);
}

I can not understand how to write the source directory. I mean, how I set the $path variable? All the files of my website is inside 'public_html' folder. Will I include 'public_html' also?

When I want to delete a folder inside 'usercontent' folder, I am writing like this :

$path = "/usercontent/somefolder"

Please answer with an example directory path.

Update: What should be the permission of the folder which will be deleted.

Hiranmoy Chatterjee
  • 195
  • 1
  • 2
  • 11
  • Maybe duplicate http://stackoverflow.com/questions/4594180/deleting-all-files-from-a-folder-using-php?rq=1 – splash58 Jun 07 '15 at 11:56
  • one more http://stackoverflow.com/questions/11613840/remove-all-files-folders-and-their-subfolders-with-php – kamal pal Jun 07 '15 at 11:58
  • I have seen these answers. Please tell me how to write the directory path correctly. – Hiranmoy Chatterjee Jun 07 '15 at 12:00
  • Do you know your path on the server? Normally something like /var/www/vhosts/yourvhost/public_html/. You should be able to see this by putting this code in your index.php: echo dirname(_ _ FILE _ _); Without the spaces inside of the dirname function. – Jim Wright Jun 07 '15 at 12:12
  • I know the path. But I am having problem to implement it. I can not understand why the code is not working. Where to set the $path variable? Please help. – Hiranmoy Chatterjee Jun 08 '15 at 03:15

0 Answers0