PHP scandir() Function: Description, Parameters, Return Values, and Example
This article explains PHP's scandir() function, detailing its syntax, parameters, return values, and provides sample code demonstrating directory listing with optional sorting and context options, along with the expected output for developers.
The scandir() function in PHP returns an array containing the files and directories within a specified directory.
Parameters
directory – The path of the directory to be scanned.
sorting_order – Optional. Set to 1 for descending alphabetical order; default is ascending.
context – Optional stream context resource (see the Streams API section of the manual).
Return value
On success, an array of directory entries is returned; on failure, FALSE is returned.
Example
<?php
$dir = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);
print_r($files1);
print_r($files2);
?>Output
Array
(
[0] => .
[1] => ..
[2] => bar.php
[3] => foo.txt
[4] => somedir
)Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.