-3
I need to find out if a folder is empty or not using PHP, because I have an image registration system, only when I delete the images from the folder, it is still there.
My goal is whenever there are no more photos in the folder it is automatically deleted, I have researched about and found nothing to help me.
Fictitious example:
$ dir = 'pastaRaiz/dirTest';
if($dir == 'empty'){
echo 'Diretório vazio deletado !';
rmdir($dir)
}
Use rmdir($dir); Reference: https://www.php.net/manual/en/function.rmdir.php
– Rodrigo Carvalho de Brito
If the directory usually contains few files, you can use the
scandir
. For directories with many files, better use the file system Iterators php.– bfavaretto
I already got what I wanted using scandir() . Thanks for the help. <? php echo '<H1>Folder:</H1><br>'; $dir = './Test folder'; $files1 = scandir($dir); // $files2 = scandir($dir, 1); print_r($files1); echo "<br>"; echo $files1[3]; echo "<br>"; if(!$files1[3]){ rmdir($dir); echo 'Deleted folder ! '; }Else{ echo "<br>"; echo 'Content archive; } ?>
– Fabio augusto cantarelli