?
Solved

Reading some file names on the server and creating a variable

Posted on 2007-09-27
1
Medium Priority
?
254 Views
Last Modified: 2010-05-19
I have some files in the "./files/" directory.
Some of these files are named "random_001.jpg", "random_002.jpg", "random_003.jpg" etc...

I need PHP to look in this directory and find all of the "random_XXX.jpg" files and find the last one (the highest numbered file).
I need that number as a variable.

On top of that...
If the number is "005", for instance, I only want the variable to be "5"
If the number is "020", I want the variable to be "20" and so on.

Can someone throw that little script together?

0
Comment
Question by:innercproductions
1 Comment
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 19975668
<?php
  $dir = "./files/";
    if ($dh = opendir($dir)) {
      while ( ($file = readdir($dh)) !== false) {
       
        echo "Looking at $dir/$file<br>\n";
        if (is_file("$dir/$file")
            && is_readable("$dir/$file")
            && preg_match('@random_\d\d\d\.jpg$@i',$file)
           ) {
          $fileNum = preg_replace('@random_0*(\d{0,3})\.jpg@','$1',$file);
          echo "File number is $fileNum<br>\n";
          if (!isset($maxNum) || $fileNum > $maxNum) {
            $maxNum = $fileNum;
            echo "Max number is now $maxNum<br>\n";
            echo "File with this number is $dir$file<br>\n";
          }
        }
      }
      closedir($dh);
    }

?>

outputs:

Looking at ./files//.
Looking at ./files//..
Looking at ./files//random_123.jpg
File number is 123
Max number is now 123
File with this number is ./files/random_123.jpg
Looking at ./files//random_034.jpg
File number is 34
Looking at ./files//random_245.jpg
File number is 245
Max number is now 245
File with this number is ./files/random_245.jpg
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question