List Files and Directories using glob in PHP - 7-Views
List Files and Directories using glob in PHP
 
                    
                
                        This function allows us to perform a search for path names using wildcards. which is similar to the rules used by common shells.
Having the following parameters:
- $pattern(mandatory): The search pattern
- $flags(optional): One or more flags.- GLOB_MARK– Adds a slash to each directory returned
- GLOB_NOSORT– Return files as they appear in the directory (no sorting). When this flag is not used, the pathnames are sorted alphabetically
- GLOB_NOCHECK– Return the search pattern if no files matching it were found
- GLOB_NOESCAPE– Backslashes do not quote metacharacters
- GLOB_BRACE– Expands {a,b,c} to match ‘a’, ‘b’, or ‘c’
- GLOB_ONLYDIR– Return only directory entries which match the pattern
- GLOB_ERR– Stop on read errors (like unreadable directories), by default errors are ignored.
 
Here is the example.
# Example1 : showing all html files in current directory.
<?php
$files = glob("*.html");
# Example2 : showing all files and directories in current directory.
<?php
$folder_files = glob("*");
Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.