JMair Posted June 1, 2009 Share Posted June 1, 2009 I have a set of folders simply named 1, 2, 3, 4, 5 - 10. I need a way to check the number of files in folder 1, and if that number is equal to or greater than 10 files, then check folder 2 and so on untill a folder has less than 10 files. When it finds a folder with less than 10, it declairs a variable and echos that folder name. How would I go about it? And thank you kindly for the help! Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/ Share on other sites More sharing options...
JMair Posted June 1, 2009 Author Share Posted June 1, 2009 Forgot what I do have so far. <?php $num = 1; $total = 0; while ($total <=10) { $file = scandir("images/stories/gallery/2009/".$num); foreach($file as $key => $value) { if(is_file($value)) { $total++; // Counter echo "$value<br />\n"; $num++; } } } echo "<p>Total files: $total</p>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-846969 Share on other sites More sharing options...
lonewolf217 Posted June 1, 2009 Share Posted June 1, 2009 i think this would work <?php $total=0; while($total <= 10) { $files = scandir("images/stories/gallery/2009/".$total); $count = count($files); if($count < 10) { break; } else { $total++; } } echo "Folder: images/stories/gallery/2009/" . $total . " has more than 10 files "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-846982 Share on other sites More sharing options...
laffin Posted June 1, 2009 Share Posted June 1, 2009 <?php $base='./'; // Remember that this shud have the ending / $files = scandir($base); foreach($files as $key => $sub) { if(preg_match('/^\d*$/',$sub) && is_dir($sub)) // Only do folders with numbers only { $subfiles=scandir("{$base}{$sub}"); $count=0; foreach($subfiles as $sf) if(is_file($s2)) $count++; if($count<10) { $subfolder=$sub; break; } } } if(isset($subfolder)) echo "{$sub} is next available dir"; else echo "No available dir"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-846996 Share on other sites More sharing options...
JMair Posted June 1, 2009 Author Share Posted June 1, 2009 <?php $base='./'; // Remember that this shud have the ending / $files = scandir($base); foreach($files as $key => $sub) { if(preg_match('/^\d*$/',$sub) && is_dir($sub)) // Only do folders with numbers only { $subfiles=scandir("{$base}{$sub}"); $count=0; foreach($subfiles as $sf) if(is_file($s2)) $count++; if($count<10) { $subfolder=$sub; break; } } } if(isset($subfolder)) echo "{$sub} is next available dir"; else echo "No available dir"; ?> Thank you for the quick reply! I've tested this and it does find the first folder named 1. I've filled folder 1 withh 10 files and the script is not bouncing to folder #2. Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-847010 Share on other sites More sharing options...
lonewolf217 Posted June 1, 2009 Share Posted June 1, 2009 did you try mine ? Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-847018 Share on other sites More sharing options...
JMair Posted June 1, 2009 Author Share Posted June 1, 2009 did you try mine ? Yes I did, and thank you. I think the problem for me is there needs to be two while statements and I'm not sure how the scope works with php in relation to this. I would imagine one WHILE would handle the folder name for the path, and another WHILE for the file count, for example $num = 1; while ($num <= 10): echo "Folder: images/stories/gallery/2009/" .$num; //the check number While here to echo $num if file total < 10. $num++; endwhile; At least this is how my simple brain works from 2 years of C about a million years ago. Your script shares $total with both (from what I can tell). Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-847040 Share on other sites More sharing options...
lonewolf217 Posted June 1, 2009 Share Posted June 1, 2009 When it finds a folder with less than 10, it declairs a variable and echos that folder name well my script fulfilled the action that you were asking for in your original post as far as I can tell, unless you weren't clear about what variable you are wanting to be declared. what totals are you wanting to track? the files in each individual folder and also the total number of files processed in folders up to this point ? Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-847059 Share on other sites More sharing options...
laffin Posted June 1, 2009 Share Posted June 1, 2009 I think this is going wrong way, we are dealing with unknown folders in a specific folder and processing them. but I think he wants em in sequence. so creation of new folders is simpler $base=''; $folder=0; do { $folder++; $sub="{$base}/{$folder}"; if(is_dir($sub)) { $files=scandir($sub); foreach($files => $file) if($count<=10 && is_file("{$sub}/$file")) $count++; } else { $count=FALSE; break; } } while($count>10); if($count==FALSE) echo "No available folder"; elseif($count<10) echo "Folder {$folder} available"; else echo "All Folders All Full"; This just works if the folders exists, numbered 1..xxx and will go thru them 1 by one, checking if each dir has 10 files or more. it will stop until a numbered directory is under 10 files or there are no more numbered directories to process. Shud be easy enough to modify Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-847090 Share on other sites More sharing options...
JMair Posted June 1, 2009 Author Share Posted June 1, 2009 When it finds a folder with less than 10, it declares a variable and echos that folder name well my script fulfilled the action that you were asking for in your original post as far as I can tell, unless you weren't clear about what variable you are wanting to be declared. what totals are you wanting to track? the files in each individual folder and also the total number of files processed in folders up to this point ? Oh it wouldn't be the first time I wasn't clear enough I suppose. Thank you for your patience. The goal is to find the first folder in a sequence that has less than 10 files within it. I'll be using this to determine where to put a photo as I only want 10 photos per page. Perhaps I should have added that in the original post. Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-847095 Share on other sites More sharing options...
lonewolf217 Posted June 1, 2009 Share Posted June 1, 2009 yea, okay? my script does that. when it hits the folder that has less than 10 pictures it it, it break's from the loop, so the value of $total is the folder number with fewer than 10 items. slightly confusing i guess since my echo'd output is incorrectly stating that this folder has more than 10 items, it should be saying that the folder has fewer than 10 items Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-847101 Share on other sites More sharing options...
laffin Posted June 1, 2009 Share Posted June 1, 2009 Problem is it doesnt go in sequence, reason I posted the 2nd code. 10 images per directory? I guess it depends on yer client base. if ya have <100 ppl than that may work well. but if yer allowing ppl to upload images, this may create another problem for u. mainly navigation and maintainance. Why isnt this using a database instead? Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-847148 Share on other sites More sharing options...
JMair Posted June 1, 2009 Author Share Posted June 1, 2009 yea, okay? my script does that. when it hits the folder that has less than 10 pictures it it, it break's from the loop, so the value of $total is the folder number with fewer than 10 items. slightly confusing i guess since my echo'd output is incorrectly stating that this folder has more than 10 items, it should be saying that the folder has fewer than 10 items I've been testing this script and I'm getting weird returns. folder 1 has 9 images in it. folder 2 has 12 images in it. folder 3 has no images in it. I've added an echo to it to see what it's seeing. <?php $num=1; while($total <= 10) { $files = scandir("images/stories/gallery/2009/".$num); $count = count($files); echo $count .'<br>'; if($count < 10) { break; } else { $num++; } } echo "Folder: images/stories/gallery/2009/" . $num . " is ready for a file! "; ?> The return is. -------------------------- 12 15 3 Folder: images/stories/gallery/2009/3 is ready for a file! --------------------------- weird right? I dont see hidden files in the folders. Anyhow, I took your script and played with it a bit and came up with the following. It works perfectly for what I need. THANK YOU THANK YOU! <?php $num =1; while ($num <=10){ $directory = "images/stories/gallery/2009/".$num."/"; $filecount = count(glob("" . $directory ."{*.jpg,*.JPG}",GLOB_BRACE)); if($filecount < 10) { break; } else { $num++; } } echo $filecount .'<br>'; echo $directory." is ready for a file! "; ?> Anyhow, thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/160495-solved-check-number-of-files-in-folder/#findComment-847181 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.