Backend Development 2 min read

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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP scandir() Function: Description, Parameters, Return Values, and Example

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
)
backendphpfilesystemphp-functionsscandir
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.