scandir函數
PHP scandir()函數 (PHP scandir() function)
The full form of scandir is "Scan Directory", the function scandir() is used to get the list of the files and directories available in the specified directory.
scandir的完整格式為“ Scan Directory” , 函數scandir()用于獲取指定目錄中可用的文件和目錄的列表。
Syntax:
句法:
scandir(directory, sorting_order, context);
Parameter(s):
參數:
directory – It specifies the directory name (or path) from there we have to get the list of the files and directories
directory –它指定目錄名稱(或路徑),從那里我們必須獲取文件和目錄的列表
sorting – It is an optional parameter; its default value is o (alphabetically sorting order). 1 can be used to descending order.
排序 –這是一個可選參數; 其默認值為o(按字母順序排序)。 1可用于降序。
context – It is an optional parameter; it is used to specify the context (a set of options that can modify the behavior of the stream) to the directory handle.
上下文 –它是一個可選參數; 它用于指定目錄句柄的上下文(一組可以修改流行為的選項)。
Return value:
返回值:
It returns an array of the files and directories, returns "False" on function execution failure.
它返回文件和目錄的數組,函數執行失敗時返回“ False”。
Example: PHP code to get and print the list of files and directories of a given directory
示例:獲取和打印給定目錄的文件和目錄列表PHP代碼
<?php
//directory name
$path = "/home";
//assigning the return array in $arr1
//defualt in ascending order
$arr1 = scandir($path);
//assigning the return array in $arr2
//Using 1 for descending order
$arr2 = scandir($path,1);
//printing the results
print_r($arr1);
print_r($arr2);
?>
Output
輸出量
Array
(
[0] => .
[1] => ..
[2] => folder1
[3] => folder2
[4] => folder3
[5] => main.php
)
Array
(
[0] => main.php
[1] => folder3
[2] => folder2
[3] => folder1
[4] => ..
[5] => .
)
翻譯自: https://www.includehelp.com/php/scandir-function-with-example.aspx
scandir函數