1

I want to delete a folder after deleting all the files inside it, in my codeigniter project. Lets say my folder name is upload, which is located near the application folder in my ci project.

The upload folder contains Peniyal as a sub-folder and it contains 4 images inside it. I need that 4 images to be deleted, next the sub-folder has to be deleted.upload folder should not be deleted. I am hanging my mind to do it. So far I have tried the following:-

$files = glob('./upload/Peniyal');//to get all file names
//am not sure whether the path is correctly given..
foreach($files as $file){ // iterate files one by one
    if(is_file($file))
        unlink($file); // delete file
}
$path   = './upload/Peniyal'; 
rmdir($path);

Folder Structure

Any help will be appreciated. Thx!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Deepak Keynes
  • 2,291
  • 5
  • 27
  • 56

2 Answers2

2

The glob() function matches a pattern, but you are not providing one. So if you use the pattern *.* the glob will find all files in that folder.

// match any file
$files = glob('./upload/Peniyal/*.*');

foreach($files as $file){
    if(is_file($file))
        unlink($file);
}
$path   = './upload/Peniyal'; 
rmdir($path);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

With CodeIgnitor 4:

delete_files('./path/to/directory/', true);
rmdir('./path/to/directory/');
uyghurbeg
  • 176
  • 3
  • 8