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';
}
9
  • does the PHP process have the correct ownership / file permission to delete the directory? Commented Apr 28, 2013 at 20:43
  • See this answer. Commented Apr 28, 2013 at 20:43
  • 10
    if you get it working - careful with a script that deletes arbitrary directories using a get parameter Commented Apr 28, 2013 at 20:44
  • 1
    what happens if name folder is ../../../ or something ? Commented Apr 28, 2013 at 20:45
  • +1 @PeterAjtai. Is really dangerous Commented Apr 28, 2013 at 20:46

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.