Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all files in directory with string or pattern in filename with PHP

Tags:

I'm trying to list out files in a directory (recursive or non) with PHP where filename matches a certain pattern. I've never been too great with regex so any help you could offer would be great. I could search for a literal check on the filenames returned, but I think that's not such a great idea :)

Update + Final Solution: 1/18/2011 @ 8:06 PM

I found another way to do what I was looking for once I understood regex a bit more. Granted I was entirely frustrated about where I was with regex, I get a bit of it now thanks to a friend pulling me aside to explain some of this in simpler terms than I was finding in online guides.

This solution basically checks for a specific image(s) with a leading prefix of either "prefixone" or "prefixtwo", while also verifying that it's an image of a certain type (jpg, jpeg, png) and matches any of the following formats.

Depending on the slug you passed from Wordpress (where I was using this), it would match with that regular expression. Here is a sample listing:

prefixone.123-abc._tag1.001.jpg
prefixone.345-xyz._tag1.002.jpeg
prefixtwo.123-abc._tag2._tag1.003.jpg
prefixone.123-abc._tag2.004.jpeg
prefixtwo.345-xyz._tag2._tag3._tag1.005.jpg
prefixtwo.123-abc._tag1.001.jpg
prefixone.345-xyz._tag1.001.png
prefixtwo.456-rst._tag1.001.png

All of these files that may have potentially been returned in the file listing from our opendir() function, any of these could have been a match if the slug matched. Regardless of the ordering of tagging information in the filename.

I hope this helps another user struggling with regex. It's a pain to get the hang of but once you understand a few fundamental things, the rest starts to rapidly fall into place to start building your own.

Code:

<?php
// create an array to hold directory list
$results = array();

// create a handler for the directory
$directory = $_SERVER['DOCUMENT_ROOT'].'/some/path/to/images/';
$handler = opendir($directory);

// open directory and walk through the filenames
while ($file = readdir($handler)) {

    // if file isn't this directory or its parent, add it to the results
    if ($file != "." && $file != "..") {

        // check with regex that the file format is what we're expecting and not something else
        if(preg_match('#^(prefixone|prefixtwo)[^\s]*\.'.$wordpress-slug.'\.[^\s]+(\.(jpg|jpeg|png))#', $file)) {

            // add to our file array for later use
            $results[] = $file;
        }
    }
}
?>

I didn't actually need recursive for this, but there really are plenty of recursive examples online and that was truly the least of my worries. Content isolation by pattern was the core of this task so the above code sufficed.

Sidenote:

To those that pointed out the "accepted comments" yesterday, I had no idea I was missing that and I apologize. I was having a bad day. Sorry if I seemed to snap at anyone about the comments. This is a great community and I'm happy to give back where I can, also.

like image 604
Will Ashworth Avatar asked Jan 17 '11 22:01

Will Ashworth


1 Answers

Use glob to find pathnames matching a pattern or a GlobIterator.

If you need that to be recursive use a RegexIterator and a RecursiveDirectoryIterator.

Marking this CW because the question is a sure duplicate and you can easily find examples for all of the above when using the Search function. Please do so.

like image 96
3 revs, 2 users 89% Avatar answered Sep 24 '22 11:09

3 revs, 2 users 89%