PHP - Deleting folders won't work on some cases

37 views Asked by At

I have a folder with subfolders containing all kind of files..

When I trigger action to delete them, some of them got deleted but some of them don't. What I am trying to say is that they are all pretty much same structure.. I don't know what is causing that? Is it something with timing or? I am deleting the folder with files once the zip files are created.

This is the method which is deleting folder with subfolders and files..

private function removeFiles($name)
{
    // Locate folder to remove
    $rootDirElements = [
        $this->kernel->getProjectDir(),
        $name
    ];
    $rootDir = implode(DIRECTORY_SEPARATOR, $rootDirElements);

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

And I am calling it in another method where zip logic is happening:

private function zipFolder($source, $destination, $name, $includeDir = false)
{

    $zip = new ZipArchive();
    if ($zip->open($destination, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
                    //zipping logic
                }
            }
        }
    } 

    $zip->close();

    // After zip file is created delete folder
    $this->removeFiles($name);

I don't know what is causing that?

0

There are 0 answers