PHP | scandir() Function

PHP scandir() Function

Hello dear readers! welcome back to a new section of our tutorial on PHP. In this tutorial, we are going to be discussing about the PHP scandir() Function.

The built-in PHP scanddir() function returns an array of files and directories from the passed directory.


Following below is the syntax to use this function -

array scandir ( string $directory [, int $sorting_order [, resource $context]] );

Parameter Details

Sr.NoParameter & Description


The directory that will be scanned.



It specifies the sort order. Default is 0 (ascending). If set to 1, it indicates descending order.



It specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream.

Return Value

This built-in function returns an array of filenames on success or FALSE on failure.


Try out the below example -

   $dir    = '/newfolder';
   $files1 = scandir($dir);
   $files2 = scandir($dir, 1);


When the above code is executed, it will produce the following result -

Array (
   [0] => .
   [1] => ..
   [2] => abc.php
   [3] => bbc.txt
   [4] => somedir
Array (
   [0] => somedir
   [1] => indiabbc.txt
   [2] => status999.php
   [3] => ..
   [4] => .

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP Error Handling Modules.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.