phplaraveldirectoryrmdir

Directory is empty but laravel does not erase it, throwing the error that "It is not empty"


I'm trying to delete a lot of directories that are already empty with a specific condition:

$noticias = Noticia::where('id_idioma', '2')->get();

    foreach($noticias as $noticia){

        $id = $noticia->id;
        $dirPath='images/noticias/'.$id.'/';
        rmdir($dirPath);

    }

This give me the error: ErrorException: rmdir(images/noticias/10446/): Directory not empty But Directory is EMPTY, any idea? I have also tried removing the last "/" in $dirPath.


Solution

  • I have solved using File::deletedirectory($dirPath) instead rmdir($dirPath) so:

    $noticias = Noticia::where('id_idioma', '2')->get();
    
    foreach($noticias as $noticia){
    
        $id = $noticia->id;
        $dirPath='images/noticias/'.$id.'/';
        File::deletedirectory($dirPath);
    
    }
    

    Don´t forget to add: use Illuminate\Support\Facades\File;