recursion - Issue recursively deleting subdirectories php


So I have this function I am using to delete files and subdirectories recursively. It can also delete single and an array of files, though that is not the issue. Everything works fine up until it has to delete the directories, all levels of files get deleted, yet folders will not. It will however delete the target directory if no subdirectories exist. Not sure if it's a directory handle issue, or what at this point. It is not a permissions issue although.

    public static function delete($path, $files){
    $dirHandle = opendir($path);
    if($files === '*'){ //delete all files and subdirectories
        foreach(glob("{$path}/*") as $file){
            if(self::checkExist($file, 'folder')){
                $pdp = dirname($file) . '/';
                $file = basename($file);
                self::delete($pdp, $file);
            } else {
                    //flash message: 'Sorry, something went wrong. Please contact an administrator.'
                    //log error couldnt delete file: $file
        if(count(glob("{$path}/*.*", GLOB_BRACE)) === 0){
                //flash message: 'Sorry, something went wrong. Please contact an administrator.'
                //log error couldnt remove directory: $path
        return true;

I also need to keep the checks and space for error logging, my entire Class, keeps this code 'theme' consistently.

People are also looking for solutions to the problem: php - Integrate barcode reader generate/read with Codeigniter


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.