PHP scandir() function with example

PHP scandir() function with example

PHP scandir() function: Here, we will find out about the PHP scandir() function with its syntax, parameters, returns a type, and example.

PHP scandir() function

The full type of scandir is “Output Directory“, the function scandir() is utilized to get the rundown of the records and indexes accessible in the predefined catalogue.

Syntax:

scandir(directory, sorting_order, setting); 

Parameter(s):

  • catalogue – It indicates the index name (or way) from that point we need to get the rundown of the documents and registries
  • arranging – It is a discretionary parameter; its default value is o (one after another in order arranging request). 1 can be accustomed to plummeting request.
  • setting – It is a discretionary parameter; it is utilized to determine the unique situation (a lot of alternatives that can change the conduct of the stream) to the index handle.

Return value:

It restores a variety of the documents and catalogues, returns “False” on function execution disappointment.

Example: PHP code to get and print the rundown of documents and catalogues of a given index

<?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] => .
)

Recent Posts

error: Alert: Content is protected!!