r3p0 Posted May 7, 2009 Share Posted May 7, 2009 Howdy, I use this code to include on one page all HTML files in a directory: <?php $dir = "directory/subDirectory/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if(!is_dir($file)) include_once($dir.$file); } closedir($dh); } } ?> How do I extend this to insert the following code if it finds no files in the directory? <h1>Directory is empty</h1> <h2>Thank you please. Come again.</h2> Thank you (please)! Quote Link to comment https://forums.phpfreaks.com/topic/157280-solved-return-value-if-empty-directory/ Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 scandir should work much better than that. Count how much it returns and if it's greater than 2, then it's not empty. Quote Link to comment https://forums.phpfreaks.com/topic/157280-solved-return-value-if-empty-directory/#findComment-828936 Share on other sites More sharing options...
premiso Posted May 7, 2009 Share Posted May 7, 2009 <?php $dir = "directory/subDirectory/"; if (is_dir($dir)) { $dirContents = glob($dir . "*"); if (count($dirContents) > 0) { foreach ($dirContents as $file) { if(!is_dir($file)) include_once($file); } }else { echo "<h1>Directory is empty</h1> <h2>Thank you please. Come again.</h2>"; } } ?> I prefer glob but that is how I roll. Quote Link to comment https://forums.phpfreaks.com/topic/157280-solved-return-value-if-empty-directory/#findComment-828939 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Well since people like displaying code snippets: $dir = 'directory/subDirectory/'; $files = scandir($dir); if (!empty($files) && count($files) > 2) echo 'directory not empty'; I used !empty() because I personally find count(NULL) to be ridiculous. But you can do it. I prefer not to. Quote Link to comment https://forums.phpfreaks.com/topic/157280-solved-return-value-if-empty-directory/#findComment-828943 Share on other sites More sharing options...
premiso Posted May 7, 2009 Share Posted May 7, 2009 I used !empty() because I personally find count(NULL) to be ridiculous. But you can do it. I prefer not to. Incase that was directed towards my code... glob returns an empty array. So it is not counting a "null" variable, it is counting an array that contains no items. Just an FYI on that. Quote Link to comment https://forums.phpfreaks.com/topic/157280-solved-return-value-if-empty-directory/#findComment-828949 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 No, I was not. scandir can return a null value. Quote Link to comment https://forums.phpfreaks.com/topic/157280-solved-return-value-if-empty-directory/#findComment-828952 Share on other sites More sharing options...
r3p0 Posted May 7, 2009 Author Share Posted May 7, 2009 This forum is great and "you people" are amazing! Thank you for displaying code snippets. I'm curious about why "count(NULL)" is ridiculous? Quote Link to comment https://forums.phpfreaks.com/topic/157280-solved-return-value-if-empty-directory/#findComment-828978 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Because you're counting nothing. It's like me telling you to count the number of marbles when you have no marbles. The situation doesn't even apply. By the way, don't request code snippets. People do them when they feel up to it. But this isn't a request forum. Just letting you know. Quote Link to comment https://forums.phpfreaks.com/topic/157280-solved-return-value-if-empty-directory/#findComment-828982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.