Home > Article > Backend Development > Which PHP function reads a directory into an array
The scandir() function in PHP can read the directory into an array. The scandir() function can read the contents (files and folders) in the specified directory. If the reading is successful, it will return an array containing the names of files and folders. The syntax is "scandir(directory to be read, sort order, directory handle environment);".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
scandir() function in PHP Directories can be read into an array.
In PHP, two functions, readdir() and scandir(), are provided to read the contents of the specified directory. The scandir() function can read the contents of the directory (files and folders). name) is stored in the array.
scandir(directory,sorting_order,context);
Parameters | Description |
---|---|
directory | Required . Specifies the directories to be scanned. |
sorting_order | Optional. Specify the order of sorting. The default is 0, indicating ascending alphabetical order. If set to SCANDIR_SORT_DESCENDING or 1, it sorts alphabetically in descending order. If set to SCANDIR_SORT_NONE, unsorted results are returned. |
context | Optional. Specifies the environment for directory handles. context is a set of options that modify the behavior of the directory stream. |
scandir() function will return an array containing file and folder names if executed successfully. If the execution fails, it will return FALSE. If the parameter $directory is not a directory, the Boolean value FALSE is returned and an E_WARNING level error is generated.
Example: View the contents of the demo directory:
##
<?php $dir = 'demo/'; if(is_dir($dir)){ $arr1 = scandir($dir); $arr2 = scandir($dir, 1); } echo "<pre class="brush:php;toolbar:false">"; print_r($arr1); print_r($arr2); ?>Recommended Study: "
PHP Video Tutorial"
The above is the detailed content of Which PHP function reads a directory into an array. For more information, please follow other related articles on the PHP Chinese website!