In this article, we will see how to get all the files from the current or specified directory using the scandir() function in PHP. The scandir() function in PHP is an inbuilt function that is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories which are present inside a specified path. The directory, stream behavior, and sorting_order of the files and directories are passed as a parameter to the scandir() function and it returns an array of filenames on success, or false on failure.
Syntax:
scandir(directory, sorting_order, context);
Parameters: The scandir() function in PHP accepts 3 parameters that are listed below:
- directory: It is a mandatory parameter that specifies the path.
- sorting_order: It is an optional parameter that specifies the sorting order. Alphabetically ascending order (0) is the default sort order. It can be set to SCANDIR_SORT_DESCENDING or 1 to sort in alphabetically descending order, or SCANDIR_SORT_NONE to return the result unsorted.
- context: It is an optional parameter that specifies the behavior of the stream.
Return Value: It returns an array of filenames on success, or false on failure.
Errors And Exceptions:
- The scandir() function throws an error of level E_WARNING if the directory specified is not a directory.
- Doing a recursive scandir will on a directory that has many files will likely either slow down your application or cause a high rise in RAM consumption due to the large size of the generated array.
Approach: In order to get all the files from the particular directory, we need to specify the complete path of the file & store the path value in the variable as $mydir. Then use the scandir() function that will scan for the files in a current or specific directory & return an array of files and directories. By default, it will be aligned in the alphabetically ascending order & 0 as default sort order, 1 for sorting in alphabetically descending order & the SCANDIR_SORT_NONE for unsorted order.
Example 1: The below example illustrate the scandir() function that will scan the files & return value will be in the ascending order.
PHP
<?php
// Specifying directory
$mydir = '/docs';
// Scanning files in a given directory in ascending order
$myfiles = scandir($mydir);
// Displaying the files in the directory
print_r($myfiles);
?>
Output:
(
[0] => .
[1] => ..
[2] => aboutus.php
[3] => contact.php
[4] => index.php
[5] => terms.php
)
Example 2: This example illustrates the scandir() function that will scan the files & return value will be in the descending order.
PHP
<?php
// Specifying directory
$mydir = '/docs';
// Scanning files in a given directory in descending order
$myfiles = scandir($mydir, 1);
// Displaying the files in the directory
print_r($myfiles);
?>
Output:
Array
(
[0] => terms.php
[1] => index.php
[2] => contact.php
[3] => aboutus.php
[4] => ..
[5] => .
)
Example 3: This example illustrates the scandir() function that will scan the files & return value will be in the unsorted order.
PHP
<?php
// Specifying directory
$mydir = '/docs';
// Scanning files in a given directory in unsorted order
$myfiles = scandir($mydir, SCANDIR_SORT_NONE);
// Displaying the files in the directory
print_r($myfiles);
?>
Output:
Array
(
[0] => .
[1] => ..
[2] => contact.php
[3] => terms.php
[4] => index.php
[5] => aboutus.php
)
Reference: http://php.net/manual/en/function.scandir.php
Similar Reads
PHP | readdir() Function The readdir() function in PHP is an inbuilt function which is used to return the name of the next entry in a directory. The method returns the filenames in the order as they are stored in the filenamesystem. The directory handle is sent as a parameter to the readdir() function and it returns the ent
2 min read
PHP | rmdir( ) Function The rmdir() function in PHP is an inbuilt function which is used to remove an empty directory. It is mandatory for the directory to be empty, and it must have the relevant permissions which are required to delete the directory. The directory to be deleted is sent as a parameter to the rmdir() functi
2 min read
PHP | rewinddir() Function The rewinddir() function is an inbuilt function in PHP which is used to rewind the directory handle. The rewinddir() function opens a directory and list its files, resets the directory handle, list its files once again, then finally closes the directory handle. The directory handle sent as a paramet
2 min read
PHP | readfile( ) Function The readfile() function in PHP reads a file and writes its contents directly to the output buffer (usually the browser). Itâs an easy way to show a file without reading it line by line yourself.If thereâs a problem reading the file, like if it doesnât exist, PHP will usually show an error message. T
3 min read
PHP | unlink() Function The unlink() function in PHP is used to delete a file from the filesystem. It does not delete directories. For that, you would need to use rmdir() or other methods.Syntax:unlink( $filename, $context )In this syntax:$filename: It is a mandatory parameter that specifies the filename of the file that h
3 min read
PHP | symlink( ) function The symlink() function in PHP is an inbuilt function which is used to create a symbolic link for a target which already exists. It helps to create a specific name link for a target. The target and link names are sent as parameters to the symlink() function and it returns True on success and False on
2 min read
PHP rename( ) Function The rename() function in PHP is an inbuilt function that is used to rename a file or directory. It attempts to change an old name of a file or directory with a new name specified by the user and it may move between directories if necessary. If the new name specified by the user already exists, the r
3 min read
PHP | opendir() Function The opendir() function in PHP is an inbuilt function which is used to open a directory handle. The path of the directory to be opened is sent as a parameter to the opendir() function and it returns a directory handle resource on success, or FALSE on failure. The opendir() function is used to open up
2 min read
PHP | ftp_pwd() function The ftp_pwd() function is an inbuilt function in PHP which returns the current directory name of the specified FTP connection. This function was introduced in PHP 4. Syntax: string ftp_pwd( $ftp_conn ) Parameter: This function accepts single parameter $ftp_conn which is used to specify the used FTP
2 min read