InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Getting a List of Filenames Matching a Pattern in PHP
PHP

Getting a List of Filenames Matching a Pattern in PHP

InfinityCoder December 26, 2016

You want to find all filenames that match a pattern.

Use a FilterIterator subclass with DirectoryIterator. The FilterIterator subclass needs its own accept() method that decides whether or not a particular value is acceptable.
To only accept filenames that end with common extensions for images:

1
2
3
4
5
6
7
8
class ImageFilter extends FilterIterator {
   public function accept() {
       return preg_match('@\.(gif|jpe?g|png)$@i',$this->current());
   }
}
foreach (new ImageFilter(new DirectoryIterator('/usr/local/images')) as $img) {
   print "<img src='".htmlentities($img)."'/>\n";
}

The FilterIterator encloses a DirectoryIterator and only allows certain elements to emerge.

It’s up to the accept() method to return true or false to indicate whether a particular element (accessed with $this->current()) is OK.

In the Solution, accept() uses a regular expression to make that determination, but your code can use any logic you like.
If your pattern can be expressed as a simple shell glob (e.g., *.*), use the glob() function to get the matching filenames.

For example, to find all the text files in a particular directory:

1
2
3
4
foreach (glob('/usr/local/docs/*.txt') as $file) {
  $contents = file_get_contents($file);
  print "$file contains $contents\n";
}

The glob() function returns an array of matching full pathnames. If no files match the pattern glob(), returns false.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Creating a Resource in PHP
You want to let people add a new resource to …

Creating a Resource in PHP

Validating Form Input: Email Addresses in PHP
You want to know whether an email address a user …

Validating Form Input: Email Addresses in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap