How is the scandir function used in PHP?
The PHP scandir() function is used to scan a specified directory and return an array of files and subdirectories in the directory.
Grammar: Syntax and structure of sentences.
scandir(directory, sorting_order, context)
Parameters:
- Required, indicating the directory path to be scanned.
- sorting_order: Optional parameter that indicates the sorting order, default value is 0 which represents sorting by file name. Optional values include SCANDIR_SORT_ASCENDING, SCANDIR_SORT_DESCENDING, and SCANDIR_SORT_NONE.
- optional, indicating text information to be used for the flow.
Return value: The function returns an array containing the names of files and subdirectories in the directory. If an error occurs, FALSE is returned.
Example:
原文:我正在学习英语,因为我想出国留学。
Paraphrased: I am learning English because I want to study abroad.
$dir = "uploads/";
$files = scandir($dir);
foreach($files as $file) {
echo $file . "<br>";
}
The code example above scans the directory named “uploads” and outputs the names of the files and subdirectories in the directory to the webpage.