- 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 exit()
- PHP exit() vs. break
- PHP isset()
- 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 mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- 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 Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- 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 and 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 the above PHP example on the glob() function is:
And here is the snapshot of the current directory, where all these files are available:
In the above example, consider the following code:
glob("*.*")
The * is used in the pattern to get all. That is, the first * refers to the name, and the second * refers to the extension. Therefore, if we decode the code, then it will be like glob("AllFiles.AllExtensions"). This is not the code, but I've written it 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, which prints the list of all files available in the 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 the above PHP program should be:
PHP glob() Syntax
The syntax of the 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 or directories.
Note: The flags parameter is used when we need to apply some extra features to search for the files or 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 or folder.
- GLOB_NOCHECK: Used to return the search pattern if no match is 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 and 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 the above PHP example is:
PHP glob(): Add a slash to each returned directory or 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 directories or folders.
PHP glob(): Return the search pattern itself if not match is found
<?php $files = glob("*.dat", GLOB_NOCHECK); for($i=0; $i<count($files); $i++) { echo $files[$i]; echo "<br>"; } ?>
The output produced by the 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 the above PHP example is given in the following snapshot:
« Previous Tutorial Next Tutorial »