Jump to content

[SOLVED] Get highest file name


aQ

Recommended Posts

Something like this:

 

$dir = $_SERVER['DOCUMENT_ROOT'] . '/phpfolder';

$dirHandle = opendir($dir);

$highnumber = 0;

while ($file = readdir($dirHandle)) {

if(!is_dir($file)) {

$num = substr(strrchr($file, "_"), 0);

$number = $str_replace('.php','',$num);

if ($highnumber < $number) {

$highnumber = $number;

}

}

}

closedir($dirHandle);

Thank you, but there is an error:

 

Fatal error: Function name must be a string in /path/to/script.php on line 9

 

The script:

<?php

$dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/';
$dirHandle = opendir($dir);
$highnumber = 0;
while ($file = readdir($dirHandle)) {
   if(!is_dir($file)) {
      $num = substr(strrchr($file, "_"), 0);
      $number = $str_replace('.php','',$num);
      if ($highnumber < $number) {
         $highpg = $file;
      }
   }
}
closedir($dirHandle);

print $highpg;

?>

simply replace $str_replace with str_replace.

 

<?php

$dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/';
$dirHandle = opendir($dir);
$highnumber = 0;
while ($file = readdir($dirHandle)) {
   if(!is_dir($file)) {
      $num = substr(strrchr($file, "_"), 0);
      $number = str_replace('.php','',$num);
      if ($highnumber < $number) {
         $highpg = $file;
      }
   }
}
closedir($dirHandle);

print $highpg;

?>

<?php
$dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/';
$dirHandle = opendir($dir);
$highnumber = 0;
while (false !== ($file = readdir($dirHandle))) {
   if(is_dir($file)) {
      $num = substr(strrchr($file, "_"), 0);
      $number = str_replace('.php','',$num);
      if ($highnumber < $number) {
         $highpg = $file;
      }
   }
}
closedir($dirHandle);

print $highpg;
?>

I believe there was an accidental ! (not) operator placed in front of is_dir, try the new code.

 

EDIT: changed the while loop to use false!== which is the php standard for readdir

<?php
$dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/';
$dirHandle = opendir($dir);
$highnumber = 0;
while (false !== ($file = readdir($dirHandle))) {
   if(is_dir($file)) {
      $num = substr(strrchr($file, "_"), 0);
      $number = str_replace('.php','',$num);
      if ($highnumber < $number) {
         $highpg = $file;
         $highnumber = $number;
      }
   }
}
closedir($dirHandle);

print $highpg;
?>

 

That should most likely fix it. Assuming you have correctly entered your servers path in $dir variable.

No !is_dir($file), means that it is NOT A DIRECTORY, which is the correct if character.  Try taking the last "/" out of the $dir... so $_SERVER['DOCUMENT_ROOT'].'/my/path'.  

 

<?php

$dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/';

$dirHandle = opendir($dir);

$highnumber = 0;

while ($file = readdir($dirHandle)) {

  if(is_dir($file)) {

     $num = substr(strrchr($file, "_"), 0);

     $number = str_replace('.php','',$num);

     if ($highnumber < $number) {

        $highpg = $file;

     }

  }

}

closedir($dirHandle);

 

print $highpg;

?>

 

Ok, lets do some debugging.  Add this $count variable

 

<?php

$count = 0;

$dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/';

$dirHandle = opendir($dir);

$highnumber = 0;

while ($file = readdir($dirHandle)) {

  if(is_dir($file)) {

     $count++;

     $num = substr(strrchr($file, "_"), 0);

     $number = str_replace('.php','',$num);

     if ($highnumber < $number) {

        $highpg = $file;

     }

  }

}

closedir($dirHandle);

 

print $highpg;

print $count;

?>

 

If $count is 0, then it is not getting into the if statement at all, which means your path to directory is wrong.  If it is, it should be the number of files in that folder.  

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.