
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP Directory scandir() Function
The PHP Directory scandir() function is used to get all of the files in the current or given directory. This function returns an array of files and directories from the given directory.
The directory, stream behavior, and sorting_order of the files and directories are given as parameters to the scandir() function, which returns an array of filenames on success and false on failure.
The sorted order is alphabetical in ascending order by default. The sort order is alphabetical in descending order if the optional sorting_order is set to SCANDIR_SORT_DESCENDING. The result is unsorted if SCANDIR_SORT_NONE is used.
Syntax
Here is the syntax of the PHP Directory scandir() function −
array scandir ( string $directory [, int $sorting_order [, resource $context]] );
Parameters
The parameters are needed to use the scandir() function are mentioned below −
Sr.No | Parameter & Description |
---|---|
1 |
directory(Required) The directory that will be scanned. |
2 |
sorting_order(Optional) It specifies the sort order. Default is 0 (ascending). If set to 1, it indicates descending order. |
3 |
context(Optional) It specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream. |
Return Value
It returns an array of filenames on success, or FALSE on failure.
PHP Version
The scandir() function was introduced in core PHP 4 and is compatible with PHP 5, PHP 7, and PHP 8.
Example
We will use the PHP Directory scandir() function to list all the files and directories present in the specified and given directory path.
The program will print the content in ascending order and then in descending order.
<?php $dir = '/newfolder'; $files1 = scandir($dir); $files2 = scandir($dir, 1); print_r($files1); print_r($files2); ?>
Output
This will produce the following result −
Array ( [0] => . [1] => .. [2] => abc.php [3] => bbc.txt [4] => somedir ) Array ( [0] => somedir [1] => indiabbc.txt [2] => status999.php [3] => .. [4] => . )
Example
In the below PHP code we will list the content in descending order only using the scandir() function and using its parameter called SCANDIR_SORT_DESCENDING.
<?php $directory = "/Applications/XAMPP/xamppfiles/htdocs/mac"; $contents = scandir($directory, SCANDIR_SORT_DESCENDING); foreach ($contents as $item) { echo $item . "<br>"; } ?>
Output
This will produce the following result −
new dir myfile.txt my.php logo.gif index.php images image.gif .DS_Store .. .
Example
Here we will use scandir() function to scan the directory and with the help of is_dir() function we will verify the directories available in the given path and print their names.
<?php // enter your working directory path here $directory = "/Applications/XAMPP/xamppfiles/htdocs/mac"; // now scan the directory $items = scandir($directory); // print the directory here using foreach loop echo "Directories are: " . "<br>"; foreach ($items as $item) { $dir = "$directory/$item"; if (is_dir($dir)) { echo $item . "<br>"; } } ?>
Output
This will lead to the following outcome −
Directories are: . .. images new dir
Example
In the below PHP code we will use scandir() function to scan the directory and with the help of is_file() function we will verify the files available in the given path and show their names.
<?php // enter your working directory path here $directory = "/Applications/XAMPP/xamppfiles/htdocs/mac"; $items = scandir($directory); echo "Files are: " . "<br>"; foreach ($items as $item) { $filePath = $directory . '/' . $item; if (is_file($filePath)) { echo $item . "<br>"; } } ?>
Output
The result of this PHP code is −
Files are: .DS_Store image.gif index.php logo.gif my.php myfile.txt
Note
- scandir() returns the current directory (.) and the parent directory (..).
- An incorrect directory path given to scandir() generates an E_WARNING error and returns FALSE.
- scandir() is a shorter alternative to the time-consuming readdir() method, which reads one directory entry at a time.
- The is_file() function can be used to limit directory entries to just files.
- Use the is_dir() function to separate directories from directory entries.
- Use the array_diff() function to delete entries from the result array.
Summary
The scandir() function in PHP is useful for listing files and directories within a specified directory. It is easy to use and provides for variable sorting of results.