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.


scandir(directory, sorting_order, setting); 


  • 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

//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


    [0] => .
    [1] => ..
    [2] => folder1
    [3] => folder2
    [4] => folder3
    [5] => main.php
    [0] => main.php
    [1] => folder3
    [2] => folder2
    [3] => folder1
    [4] => ..
    [5] => .

Leave a Comment

error: Alert: Content is protected!!