Jump to content

[SOLVED] Get highest file name


aQ

Recommended Posts

Hello!

 

I have a folder with a lot of .php files in it. They are named *somestuff*_*number*.php (blabla_10.php). I want to find the file with the highest value in its name. Is this possible?

 

Thank you.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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;

?>

 

Link to comment
Share on other sites

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.  

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.