We now have a youtube channel. Subscribe!

PHP | scandir() Function

PHP scandir() Function


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

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

Syntax

Following below is the syntax to use this function -

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


Parameter Details

Sr.NoParameter & Description
1

directory(Required)

The directory that will be scanned.

2

sorting_order(Optional)

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

3

context(Optional)

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 function returns an array of filenames on success or FALSE on failure.

Example

Try out the below example -

<?php
   $dir    = '/newfolder';
   $files1 = scandir($dir);
   $files2 = scandir($dir, 1);
   
   print_r($files1);
   print_r($files2);
?> 

Output

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.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain