1

I am trying to delete a folder with files inside of it but the following code does delete the files, but not the folder.

$nameFolder = $_GET['delete'];
$dir = '../projecten/projecten/'.$nameFolder.'';
$filesIN = glob($dir."/"."*");
$status = 'false';

foreach($filesIN as $files) //here i take all the files
    unlink($files);

$status = 'true';
if($status=='true'){
    rmdir($dir);
    $status = 'false';
}
doppelgreener
  • 4,809
  • 10
  • 46
  • 63
Ajeo
  • 73
  • 5
  • 11
  • does the PHP process have the correct ownership / file permission to delete the directory? – Andy Jones Apr 28 '13 at 20:43
  • See this [answer](http://stackoverflow.com/questions/1653771/how-do-i-remove-a-directory-that-is-not-empty). – jerdiggity Apr 28 '13 at 20:43
  • 10
    if you get it working - careful with a script that deletes arbitrary directories using a get parameter – Peter Ajtai Apr 28 '13 at 20:44
  • 1
    what happens if name folder is ../../../ or something ? – Ian Kenney Apr 28 '13 at 20:45
  • +1 @PeterAjtai. Is really dangerous – Axel A. García Apr 28 '13 at 20:46
  • i'm trying this now on a local server i think that php the right prommissions have – Ajeo Apr 28 '13 at 20:46
  • The `glob()` function only returns files and folders immediately within the given folder. You're not recursing to, in turn, delete the contents of _those_ folders. In addition, your program flow is weird - you set status to true, and then immediately check if it's true. That `if` block will _always_ execute. – doppelgreener Apr 28 '13 at 23:45
  • You're forgetting hidden files... glob works exactly like wildcarding at the shell prompt, and wildcards by default do **NOT** match against hidden `.whatever` files. – Marc B Apr 28 '13 at 23:55
  • Hi! My answer solve your problem? If so check as "correct", if not tell me what is missing. – Protomen Apr 12 '15 at 14:49

3 Answers3

3

[edited] Only empty directories can be deleted.

Try:

<?php
//recursively remove a directory
function rrmdir($dir) {
    foreach(glob($dir . '/' . '*') as $file) {
        if(is_dir($file)){
            rrmdir($file);
        }else{
            unlink($file);
        }
    }
    rmdir($dir);
}

//Example
$nameFolder = $_GET['delete'];
$dir = '../projecten/projecten/'.$nameFolder.'';
rrmdir($dir);
?>

source: http://www.php.net/manual/pt_BR/function.rmdir.php#108113

Protomen
  • 9,471
  • 9
  • 57
  • 124
1

I would check the file permissions. On linux:

ls -al /path/to/projecten/projecten/

In simple terms the web server user must have write access to the directory in order to delete the file, for example the user www-data. In the below example the user lt can delete the test file:

drwxrwxr-x 2 lt lt 4096 Apr 29 08:54 test

Also I don't understand this bit of code:

$status = 'true';
if($status=='true'){
   rmdir($dir);
   $status = 'false';
}

Why not just have:

rmdir($dir);

As $status will always be 'true'.

You could also try using a system call, eg:

system `rm -rf /full/path/to/projecten/projecten/$nameFolder`;

Be very careful with that system command though - If you delete the wrong directory there is no going back!

A safer system command to use if you know the directory is empty would be:

system `rmdir /full/path/to/projecten/projecten/$nameFolder`;

But as pointed out in the comments above be very careful deleting a directory based on a $_GET variable. Imagine if the GET variables was '../../projecten' especially with the 'rm -rf' system command

someuser
  • 2,279
  • 4
  • 28
  • 39
0

Not an answer, but please change:

$nameFolder = $_GET['delete'];

To:

$nameFolder = basename($_GET['delete']);

And you may want to also add a:

 if (is_dir('../projecten/projecten/'.$nameFolder) {
     // ... do stuff here
 } else {
     // not a valid path
 }
Tigger
  • 8,980
  • 5
  • 36
  • 40