Guest MrLeN Posted September 8, 2015 Share Posted September 8, 2015 I have a directory with folder numbers, ie: 1 2 3 4 ..and I have created the ability to delete folders. So 2 could be deleted and I'll wind up with: 1 3 4 ..that's all well and good, but before I added the ability to delete folders, I was simply counting the folders, adding 1 to what ever the count was, and then creating a new folder with the new number. My Problem: If I use that process, the next number will be 4 (which already exists). So now what I have decided I'm going to have to do is get all the folder numbers, then get the highest folder number, and add one to THAT number. if ($handle = opendir('.')) { while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { echo "$entry\n"; } } closedir($handle); } How can I get the highest number of $entry? Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/ Share on other sites More sharing options...
scootstah Posted September 8, 2015 Share Posted September 8, 2015 Use glob() to get the directory names and then use max() to get the highest one. Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520491 Share on other sites More sharing options...
Guest MrLeN Posted September 8, 2015 Share Posted September 8, 2015 How do I turn glob() values into an array? Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520492 Share on other sites More sharing options...
scootstah Posted September 8, 2015 Share Posted September 8, 2015 It returns an array. Return Values Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error. Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520494 Share on other sites More sharing options...
Guest MrLeN Posted September 8, 2015 Share Posted September 8, 2015 Here is the solution I came up with: $dir1 = $path . '/1'; $dir2 = $path . '/2'; $dir3 = $path . '/3'; $dir4 = $path . '/4'; $dir5 = $path . '/5'; $dir6 = $path . '/6'; $dir8 = $path . '/7'; $dir9 = $path . '/8'; $dir10 = $path . '/10'; $dir11 = $path . '/11'; $dir12 = $path . '/12'; $dir13 = $path . '/13'; $dir14 = $path . '/14'; $dir15 = $path . '/15'; $dir16 = $path . '/16'; $dir17 = $path . '/17'; $dir18 = $path . '/18'; $dir19 = $path . '/19'; $dir20 = $path . '/20'; if (!file_exists($dir1)) { echo "creating dir 1"; } else { if (!file_exists($dir2)) { echo "creating dir 2"; } else { if (!file_exists($dir3)) { echo "creating dir 3"; } else { if (!file_exists($dir4)) { echo "creating dir 4"; } else { if (!file_exists($dir5)) { echo "creating dir 5"; } else { if (!file_exists($dir6)) { echo "creating dir 6"; } else { if (!file_exists($dir7)) { echo "creating dir 7"; } else { if (!file_exists($dir8)) { echo "creating dir 8"; } else { if (!file_exists($dir9)) { echo "creating dir 9"; } else { if (!file_exists($dir10)) { echo "creating dir 10"; } else { if (!file_exists($dir11)) { echo "creating dir 11"; } else { if (!file_exists($dir12)) { echo "creating dir 12"; } else { if (!file_exists($dir13)) { echo "creating dir 13"; } else { if (!file_exists($dir14)) { echo "creating dir 14"; } else { if (!file_exists($dir15)) { echo "creating dir 15"; } else { if (!file_exists($dir16)) { echo "creating dir 16"; } else { if (!file_exists($dir17)) { echo "creating dir 17"; } else { if (!file_exists($dir18)) { echo "creating dir 18"; } else { if (!file_exists($dir19)) { echo "creating dir 19"; } else { if (!file_exists($dir20)) { echo "creating dir 20"; } } } } } } } } } } } } } } } } } } } } Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520496 Share on other sites More sharing options...
Ch0cu3r Posted September 8, 2015 Share Posted September 8, 2015 Why? Whats wrong with what scootstah told you to do. Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520497 Share on other sites More sharing options...
Guest MrLeN Posted September 8, 2015 Share Posted September 8, 2015 Why? Whats wrong with what scootstah told you to do. I do not understand what he is saying in the slightest. Not a clue.. Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520498 Share on other sites More sharing options...
scootstah Posted September 8, 2015 Share Posted September 8, 2015 (edited) Did you even bother to read the manual for the functions I gave you? If you want to hard code it, at the very least do this so it's not a maintenance nightmare: $max = 20; for ($i = 1; $i <= $max; i++) { $dir = $path . '/' . $i; if (!file_exists($dir)) { echo 'Creating dir ' . $i; break; } } Edited September 8, 2015 by scootstah Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520499 Share on other sites More sharing options...
Guest MrLeN Posted September 8, 2015 Share Posted September 8, 2015 Did you even bother to read the manual for the functions I gave you? If you want to hard code it, at the very least do this so it's not a maintenance nightmare: $max = 20; for ($i = 1; $i <= $max; i++) { $dir = $path . '/' . $i; if (!file_exists($dir)) { echo 'Creating dir ' . $i; break; } } I did look for about 5 minutes. It gave me a headache. Hence my question: "How do I turn glob() values into an array?" .. it wasn't long after that I started to feel nauseous.. so I decided to sit back, have a think -- and write up some code that I understand. I can see you're unimpressed, but my solution was to the best of my ability. I am not a very good programmer; but I try hard. Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520500 Share on other sites More sharing options...
Guest MrLeN Posted September 8, 2015 Share Posted September 8, 2015 Thanks for the short code though I'll see if I can implement it. Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520502 Share on other sites More sharing options...
Guest MrLeN Posted September 8, 2015 Share Posted September 8, 2015 That code gave me an error. Parse error: syntax error, unexpected T_INC, expecting ')' in /xxx.php on line 11 Line 11 is: for ($i = 1; $i <= $max; i++) { Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520503 Share on other sites More sharing options...
scootstah Posted September 8, 2015 Share Posted September 8, 2015 Sorry, missed a dollar. for ($i = 1; $i <= $max; $i++) { I did look for about 5 minutes. It gave me a headache. Hence my question: "How do I turn glob() values into an array?" .. it wasn't long after that I started to feel nauseous.. so I decided to sit back, have a think -- and write up some code that I understand. I can see you're unimpressed, but my solution was to the best of my ability. I am not a very good programmer; but I try hard. You're going to have to put more effort into learning programming than spending 5 minutes on a problem before giving up because you "feel nauseous". The manual and user contributed notes pretty much tells you what you need to do to use glob. <?php $dirs = array(); foreach(glob('[0-9]*', GLOB_ONLYDIR) as $dir) { if (!is_numeric($dir)) { continue; } $dirs[] = $dir; } echo max($dirs); Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520508 Share on other sites More sharing options...
Solution Guest MrLeN Posted September 9, 2015 Solution Share Posted September 9, 2015 (edited) This code works like a charm. And it's actually better than finding the highest folder, because if one of the 20 folders gets deleted, the code will replace it, instead of (theoretically) winding up at folder 2 million. There will always only be 20 folders - and never a gap in the numbers. Thanks scootash. I appreciate your help $max = 20; for ($i = 1; $i <= $max; $i++) { $dir = $path . '/' . $i; if (!file_exists($dir)) { //echo 'Creating dir ' . $i; $new_category = $i; break; } } Edited September 9, 2015 by MrLeN Quote Link to comment https://forums.phpfreaks.com/topic/298096-get-highest-folder-php/#findComment-1520530 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.