Home  >  Article  >  Backend Development  >  Which PHP function reads a directory into an array

Which PHP function reads a directory into an array

青灯夜游
青灯夜游Original
2022-05-10 16:21:471815browse

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);".

Which PHP function reads a directory into an array

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:

Which PHP function reads a directory into an array

Which PHP function reads a directory into an array##

<?php
$dir = &#39;demo/&#39;;
if(is_dir($dir)){
    $arr1 = scandir($dir);
    $arr2 = scandir($dir, 1);
}
echo "<pre class="brush:php;toolbar:false">";
print_r($arr1);
print_r($arr2);
?>

Which PHP function reads a directory into an array

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn