lpxxfaintxx Posted February 10, 2009 Share Posted February 10, 2009 I wrote a simple piece that scans the directory for images only. How would I check if there are NO IMAGES in the folder? (Note, I said no images, not files). I want to make an if statement so that if no images exist in the folder, then echo 'whatever.' Thanks Link to comment https://forums.phpfreaks.com/topic/144701-solved-how-do-i-check-if-a-directory-is-empty/ Share on other sites More sharing options...
gevans Posted February 10, 2009 Share Posted February 10, 2009 Can you show the code to scan for images? the best way will be to adapt that, or negate it Link to comment https://forums.phpfreaks.com/topic/144701-solved-how-do-i-check-if-a-directory-is-empty/#findComment-759314 Share on other sites More sharing options...
lpxxfaintxx Posted February 10, 2009 Author Share Posted February 10, 2009 Gah! SO sorry, I meant to paste the code in the original post, but I must've forgot. $featured_dir = 'albums/'.$random.'/original/'; $file_types = array('gif','jpg','png'); $dir = 'albums/'.$random.'/'; $scan = scandir($dir); for ($i=0; $i<count($scan); $i++) { if ($scan[$i] != '.' && $scan[$i] != '..' && in_array(end(explode('.', $scan[$i])), $file_types)) { echo ' <p class="photoind"> <a href="' . $featured_dir . $scan[$i] . '" rel="lightbox-'.$random.'"> <img src="'. $dir . $scan[$i] . '" alt="'. $scan[$i] . '" width="132" height="132" rel="lightbox"/> <br /> </a> </p>'; } } Link to comment https://forums.phpfreaks.com/topic/144701-solved-how-do-i-check-if-a-directory-is-empty/#findComment-759323 Share on other sites More sharing options...
gevans Posted February 10, 2009 Share Posted February 10, 2009 <?php $file_types = array('gif','jpg','png'); $dir = 'albums/'.$random.'/'; $scan = scandir($dir); $image_found = FALSE; for ($i=0; $i<count($scan); $i++) { if ($scan[$i] != '.' && $scan[$i] != '..' && in_array(end(explode('.', $scan[$i])), $file_types)) { $image_found = TRUE; echo ' <p class="photoind"> <a href="' . $featured_dir . $scan[$i] . '" rel="lightbox-'.$random.'"> <img src="'. $dir . $scan[$i] . '" alt="'. $scan[$i] . '" width="132" height="132" rel="lightbox"/> <br /> </a> </p>'; } } if(!$image_found) { //no image in directory } Link to comment https://forums.phpfreaks.com/topic/144701-solved-how-do-i-check-if-a-directory-is-empty/#findComment-759324 Share on other sites More sharing options...
Psycho Posted February 11, 2009 Share Posted February 11, 2009 I'd suggest using glob() instead of scandir(). Then you can return all the image files and nothing else from the directory without a loop. And if the results are empty - then there are no images. Give this a try $images = glob('{*.jpg,*.gif,*.png}', GLOB_BRACE); Link to comment https://forums.phpfreaks.com/topic/144701-solved-how-do-i-check-if-a-directory-is-empty/#findComment-759327 Share on other sites More sharing options...
printf Posted February 11, 2009 Share Posted February 11, 2009 I would use a image function to do the check... <?php $out = 0; $dir = './images'; $files = glob ( $dir . '/*' ); foreach ( $files AS $file ) { if ( ! is_dir ( $file ) ) { if ( false !== @getimagesize ( $file ) ) { $out++; } } } if ( $out == 0 ) { echo 'no images in directory: ' . $dir; } else { echo '(' . $out . ') total images found in directory: ' . $dir; } ?> Link to comment https://forums.phpfreaks.com/topic/144701-solved-how-do-i-check-if-a-directory-is-empty/#findComment-759328 Share on other sites More sharing options...
lpxxfaintxx Posted February 11, 2009 Author Share Posted February 11, 2009 Thanks everyone--I went ahead with gevans' solution. Link to comment https://forums.phpfreaks.com/topic/144701-solved-how-do-i-check-if-a-directory-is-empty/#findComment-759340 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.