- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP glob() | Get List of all Files/Directories
The PHP glob() function is used when we need to get the list of files (based on the pattern given to it) in the form of arrays. For example:
<?php $files = glob("*.*"); print_r($files); ?>
The output produced by above PHP example on glob() function is:
And here is the snapshot of the current directory, where all these files are available:
In above example, consider the following code:
glob("*.*")
The * is used in pattern to get all. That is, the first * refers to the name, and the second * refers to extension. Therefore if we decode the code, then it will be like glob("AllFiles.AllExtensions"), this is not the code, but I've written this for you to get the scenario. So, the glob("*.*") is used to get all files.
Note - If you want to get only .txt files, then use glob("*.txt"). If you want to list only .php files, then use glob("*.php")
The same program can also be created in this way, that prints the list of all files available in current directory, one by one:
<?php $files = glob("*.*"); echo "<p>List of all files:</p>"; for($i=0; $i<count($files); $i++) { echo $files[$i]; echo "<br>"; } ?>
Now the output of above PHP program should be:
PHP glob() Syntax
The syntax of glob() function in PHP, is:
glob(pattern, flags)
The first (pattern) parameter is required, whereas the last (flags) parameter is optional.
Note - The pattern parameter is used to define the pattern to search for the files/directories
Note - The flags parameter is used when we need to apply some extra features to search for the files/directories, using any of the following:
- GLOB_BRACE - Used to expand {a, b, c} to match 'a', 'b', or 'c'
- GLOB_ERR - Used to stop errors
- GLOB_MARK - Used to insert a slash to each returned directory/folder
- GLOB_NOCHECK - Used to return the search pattern, in case if no match were found
- GLOB_NOSORT - Used to return files as they are in the directory without sorting
- GLOB_NOESCAPE - Backslashes do not quote metacharacters in this case
- GLOB_ONLYDIR - Used when we need to get the list of directories only
PHP glob() - List all Files/Folders
<?php $files = glob("*"); echo "<p>List of all Files and Folders:</p>"; for($i=0; $i<count($files); $i++) { echo $files[$i]; echo "<br>"; } ?>
The output produced by above PHP example, is:
PHP glob() - Add Slash to Each Returned Directory/Folder
<?php $files = glob("*", GLOB_MARK); echo "<p>List of all Files and Folders:</p>"; for($i=0; $i<count($files); $i++) { echo $files[$i]; echo "<br>"; } ?>
The output should be:
Notice the slash after dashboard, img, webalizer, and xampp. These are the directory/folder.
PHP glob - Return Search Pattern itself if No Match Found
<?php $files = glob("*.dat", GLOB_NOCHECK); for($i=0; $i<count($files); $i++) { echo $files[$i]; echo "<br>"; } ?>
The output produced by above example should be *.dat
PHP glob() - List Directories Only
<?php $files = glob("*", GLOB_ONLYDIR); echo "<p>List of all Directories:</p>"; for($i=0; $i<count($files); $i++) { echo $files[$i]; echo "<br>"; } ?>
The output of above PHP example is given in the following snapshot:
« Previous Tutorial Next Tutorial »