PHP: Get files with higher number in filename using glob()

Posted on 2012-08-31
Last Modified: 2012-09-16
Using PHP's function glob(), how can I get all files that contain a number greater than 12 in a specific part of the filename?

$m = glob('xyz/*/Item_[ Number Greater Than 12  ].txt');

Open in new window

Question by:hankknight
    LVL 42

    Assisted Solution

    by:Chris Stanyon
    Not sure you can do it with just glob. You may need to throw in a preg_match:

    $files = glob('*.txt');
    $matchingFiles = array();
        foreach ($files as $file) {
    		if (preg_match("/Item_([0-9])\.txt/",$file,$matches)) {
    			if ($matches[1] > 12) { $matchingFiles[] = $file; }	

    Open in new window

    LVL 12

    Assisted Solution

    by:Mohamed Abowarda
    The idea is that you go through each file and check if the file contains a number more than 12.

    Take a look over scandir():
    LVL 107

    Assisted Solution

    by:Ray Paseur
    It cannot be done with glob() alone.

    If you want to post a list of file names that are representative of your directory, I'll be glad to write a function to return an array of those that match your criteria.
    LVL 25

    Accepted Solution

    it can be done with glob providing that you know the maximum number of digits of the number or providing that you can accept junk after the number

    the pattern would be like this :

    or like this if you do not want junk, but know the number of digits does not exceed 4

    and you need the flag GLOB_BRACE (please check the exact name of the flag i'm not sure)

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    The viewer will learn how to dynamically set the form action using jQuery.
    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.

    745 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

    13 Experts available now in Live!

    Get 1:1 Help Now