0

I am able to delete files inside my upload folder inside my server using PHP unlink() see below code, but script only deletes files, how to include and delete folders?

$files = glob('upload/*'); // get all file names
foreach($files as $file){ // iterate files
  if(is_file($file))
    unlink($file); // delete file
}

I found this code but it gives me a permission denied error.

array_map('unlink', glob("upload/*"));

And used this code below

function deleteFiles($directory) {
    $recursive = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
    $files = new RecursiveIteratorIterator($recursive, RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($files as $file) {
        if ($file->isDir()) {
            rmdir($file->getRealPath());
        } else {
            unlink($file->getRealPath());
        }
    }
    rmdir($directory);
}

deleteFiles('upload');

But permission denied error displays

Warning: rmdir(upload): Permission denied in

I am trying this code my self on my localhost and my user account is administrator.

KaoriYui
  • 912
  • 1
  • 13
  • 43
  • Check [`rmdir()`](http://php.net/manual/en/function.rmdir.php). – Tigger May 19 '17 at 23:12
  • 2
    Did you read this: http://php.net/manual/bg/function.unlink.php Also there is an example of recursivly deleting all files and dirs. Your permission denied is maybe because of trying to delete `..` directory. – Todor Simeonov May 19 '17 at 23:13
  • Possible duplicate of [Delete directory with files in it?](http://stackoverflow.com/questions/3349753/delete-directory-with-files-in-it) – Don't Panic May 19 '17 at 23:18
  • @Don't Panic I am using the top answer code but got this error instead `'RecursiveDirectoryIterator::__construct(upload\*,upload\*): Permission denied' ` – KaoriYui May 19 '17 at 23:24
  • 1
    Well, even after using code that _should_ delete a folder rather than a file, it's still certainly possible that the user account you're executing this with doesn't have the necessary permission to do so. – Don't Panic May 19 '17 at 23:27

4 Answers4

1

Ok so after modifying the function deleteFiles() i need to set my directory to 0777 using below code

chmod($directory,0777);

Then after deleting i need to remake the directory again using mkdir below is the modified code.

function deleteFiles($directory) {
    chmod($directory,0777);
    $recursive = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
    $files = new RecursiveIteratorIterator($recursive, RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($files as $file) {
        if ($file->isDir()) {
            rmdir($file->getRealPath());
        } else {
            unlink($file->getRealPath());
        }
    }
    rmdir($directory);
}

deleteFiles('upload');

mkdir("upload", 0700);
KaoriYui
  • 912
  • 1
  • 13
  • 43
  • if your query was solved please accept your answer and upvote the ones that were useful, this's how SO works. – Junius L May 20 '17 at 15:36
0

Use rmdir. Documentation This will allow you to delete empty directories in PHP.

0

You could create a function that would remove files (unlink) and directories (rmdir) recursively.

There is a library that lets you work with the file system easily. It is a Symfony component called Filesystem. You can use it as an independent component easily. It has remove method that can do what you need: http://symfony.com/doc/current/components/filesystem.html#remove

dmnptr
  • 4,258
  • 1
  • 20
  • 19
0

Use the following function which use RecursiveDirectoryIterator

The RecursiveDirectoryIterator provides an interface for iterating recursively over filesystem directories.

function deleteFiles($directory) {
    $recursive = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
    $files = new RecursiveIteratorIterator($recursive, RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($files as $file) {
        if ($file->isDir()) {
            rmdir($file->getRealPath());
        } else {
            unlink($file->getRealPath());
        }
    }
    rmdir($directory);
}

Call it like

 deleteFiles('uploads');
Junius L
  • 15,881
  • 6
  • 52
  • 96
  • when you downvote, try to comment in doing so you are helping me to improve my answer and not to repeat my mistakes. – Junius L May 20 '17 at 09:30