Reading some file names on the server and creating a variable

Posted on 2007-09-27
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?

Question by:innercproductions
    1 Comment
    LVL 34

    Accepted Solution

      $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";



    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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to count occurrences of each item in an array.

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now