Take the 2-minute tour ×
$blogDir = 'blog/';
$blogDirHandle = opendir( $blogDir );
$checkingFile = '';
$number = 0;
$codeNumber = '-'.$number.'-';
if( $blogDirHandle = opendir( 'blog/' ) )
    while( false !== ( $checkingFile = readdir( $blogDirHandle ) ) ) 
        if( $checkingFile != '.' && $checkingFile != '..' && !is_dir( $blogDir.$checkingFile ) && false !== strpos( $checkingFile, $codeNumber ) )
closedir( $blogDirHandle );

The code above is supposed to check, if $blogDir has a file that begins with the current $codeNumber. If it does, then increment $number ( thus changing $codeNumber ), and then loop the check until there's no more files that start with the $codeNumber. After this, I make multiple files, which are named with the new $codeNumber.

But for some reason it does not work.

If I have only one file in the directory, called -0-filename.php, it does increment the value, and the next file I create has -1- as it's prefix. But after creating the second file, it won't increment $number. It just makes duplicate-prefix-having files ( i.e. -0-filename.php, -1-othername.php, and -1-thirdname.php )

Any help?

share|improve this question
Have you echoed the file names coming out of the loop (i.e. $checkFile)? perhaps the -1- file is retrieved first, while $number = 0 which causes $number to increment and then -0- file is retrieved and then $checkingFile = '/blog/-0-...' and $number = 1 and therefore the condition will fail and your loop will end leaving $number = 1... just a guess. –  Yaniro Mar 10 '12 at 20:16
At what point should I echo $checkFile? –  user1219742 Mar 10 '12 at 20:21
Inside the while() –  Yaniro Mar 10 '12 at 20:23
I can't seem to see anything wrong here off the bat. What happens if you don't have any files inside blog/? Does it create the -0-filename.php file? –  rgin Mar 10 '12 at 20:27
@Yaniro - I added 'echo $checkingFile;' before the if-statement, inside the while-loop, and the output was: ..-1-filename.php-1-secondfilename.php.-0-(firstfileuploaded).phpimages | ATrgin - Yes it does. –  user1219742 Mar 10 '12 at 20:31

2 Answers 2

up vote 1 down vote accepted

Let me repeat myself, from your previous question: gather all files to an array and then operate on it.

Lets say your folder contains 2 files:


But for some reason your server is listing this directory in reversed order, so in the first iteration you are checking -0- but current file is -1-filename.txt, nothing happens, number is not incremented and readdir is moving to the next file that is -0-filename.txt. Now you have match, number is incremented, but there are no more files to read - readdir loop has ended and you are left with number = 1.

share|improve this answer
+1, you and @Yaniro were right. –  davidethell Mar 10 '12 at 20:57

Here's another way to do it:

$lastNum = 0;

if ( ( $files = scandir( 'blog/' ) ) !== FALSE && count( $files ) > 0 )
    $lastNum = substr( $files[ count( $files ) - 1 ], 1, 1 );

scandir() returns a sorted array of file names extracted from the specified path. The code then takes the last entry of the sorted array (which, provided that the files in the directory are GUARANTEED(!) to follow the above mentioned convention, will be the one with the highest number in the file name) and then extracts the second character from the file names which is supposed to be the file number indicator.

share|improve this answer
Sorry, I was gone for a few days. - Where should I put that code of yours/what should I replace? –  user1219742 Mar 16 '12 at 17:19
:), replace everything. this code is supposed to do exactly what you need, play with it and check it out. –  Yaniro Mar 16 '12 at 21:13

Your Answer

