Jump to content

Get highest folder PHP


Guest MrLeN
Go to solution Solved by Guest MrLeN,

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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";
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
Link to comment
Share on other sites

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 by scootstah
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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++) {
Link to comment
Share on other sites

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);
Link to comment
Share on other sites

  • Solution

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 by MrLeN
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.