postbil.com Posted March 20, 2010 Share Posted March 20, 2010 Hello phpfreaks .. I'm trying to learn php .. and now I've made a small function which looks in the folder, the folder I empty it should write "The folder is empty", and if the folder is not empty, it should write "folder is not empty". But no matter how many Filder are in the folder, get the same result "folder is not empty" .. I hope someone can help me and tell what I'm doing wrong. Here is my function: <?php function insertFile ($imageFunction){ // First make sure the folder is empty $dh = opendir($imageFunction['dir']); // Create a array for all file names in the folder $files = array(); $files[] = readdir($dh); // If the count of filenames is more than 0 if(count($file) > 0) { echo 'The folder is not empty'; }else{ echo 'The folder is empty.'; } echo count($file); closedir($dh); } ?> Postbil.com Quote Link to comment https://forums.phpfreaks.com/topic/195920-empty-folder/ Share on other sites More sharing options...
Ruzzas Posted March 20, 2010 Share Posted March 20, 2010 Have a look at this: function is_empty_folder($dir) { if (is_dir($dir)) { $dl=opendir($dir); if ($dl) { while($name = readdir($dl)) { if (!is_dir("$dir/$name")) { //<--- corrected here return false; break; } } closedir($dl); } return true; } else return true; } Quote Link to comment https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029145 Share on other sites More sharing options...
teamatomic Posted March 20, 2010 Share Posted March 20, 2010 Even an empty folder has two files in it. "." and "..", filter those out. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029157 Share on other sites More sharing options...
MadTechie Posted March 20, 2010 Share Posted March 20, 2010 To follow on from teamatomic post, as your counting from readdir your need to expect the . and .. file nodes so update if(count($file) > 0) { to if(count($file) > 2) { Quote Link to comment https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029204 Share on other sites More sharing options...
skurai Posted March 20, 2010 Share Posted March 20, 2010 Or you can strip out those files from being seen: if(is_dir($dir)){ $dir_array = scandir($dir); foreach($dir_array as $file) { if(stripos($file, '.') > 0) { echo "There are files in the folder"; } else { echo "There are no files"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029209 Share on other sites More sharing options...
postbil.com Posted March 20, 2010 Author Share Posted March 20, 2010 Thank you all .. But I have just two more questions .. How can it be that readdir () will always be '. " and '..' even if the folder is empty? and how can I give you poing? Quote Link to comment https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029292 Share on other sites More sharing options...
teamatomic Posted March 20, 2010 Share Posted March 20, 2010 Yes, even an empty folder will have "." and ".." it a feature of the OS. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029322 Share on other sites More sharing options...
greatstar00 Posted March 20, 2010 Share Posted March 20, 2010 Or you can strip out those files from being seen: if(is_dir($dir)){ $dir_array = scandir($dir); foreach($dir_array as $file) { if(stripos($file, '.') > 0) { echo "There are files in the folder"; } else { echo "There are no files"; } } } folder "." is go back 1 folder back, like u are at c:\windows\system32, it will go back to c:\windows folder ".." is go back to root, i think, so, u will be at c:\ after u clicked it most files has extensions, so, your way just strip many files out too Quote Link to comment https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029331 Share on other sites More sharing options...
skurai Posted March 21, 2010 Share Posted March 21, 2010 Or you can strip out those files from being seen: if(is_dir($dir)){ $dir_array = scandir($dir); foreach($dir_array as $file) { if(stripos($file, '.') > 0) { echo "There are files in the folder"; } else { echo "There are no files"; } } } folder "." is go back 1 folder back, like u are at c:\windows\system32, it will go back to c:\windows folder ".." is go back to root, i think, so, u will be at c:\ after u clicked it most files has extensions, so, your way just strip many files out too I have used this method many different times on many different projects and it has never stripped out any proper files out of the count Quote Link to comment https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029346 Share on other sites More sharing options...
teamatomic Posted March 21, 2010 Share Posted March 21, 2010 folder "." is go back 1 folder back, like u are at c:\windows\system32, it will go back to c:\windows folder ".." is go back to root, i think, so, u will be at c:\ after u clicked it No. dot refers to the current folder. dot-dot refers to the parent folder. And tell me, how would you ever click on one. The file system itself uses them for traversing the directory tree. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/195920-empty-folder/#findComment-1029348 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.