简体   繁体   中英

Codeigniter : Delete directory after deleting all files

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);

资料夹结构

Any help will be appreciated. Thx!

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM