Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

scandir()

Example

List the files and directories within the images directory.

<?php
$dir = “/images/”;

// Sort in ascending order – this is default
$a = scandir($dir);

// Sort in descending order
$b = scandir($dir,1);

print_r($a);
print_r($b);
?>

Result:

Array
(
[0] => .
[1] => ..
[2] => cat.gif
[3] => dog.gif
[4] => horse.gif
[5] => myimages
)
Array
(
[0] => myimages
[1] => horse.gif
[2] => dog.gif
[3] => cat.gif
[4] => ..
[5] => .
)

Definition and Usage

The scandir() function returns an array containing the files and directories within the specified directory.

Syntax

scandir(directoryordercontext)

Parameter Values

 

Parameter

Description

directory

Required. Specifies the directory to scan.

order

Optional. Specifies the context for the directory handle, which consists of options that can modify the stream’s behavior.

context

Optional. Specifies the context for the directory handle, which consists of options that can modify the stream’s behavior.

Technical Details

Return Value:

Returns an array of files and directories on success, or FALSE on failure. Throws an E_WARNING if the path is not a valid directory.

PHP Version:

5.0+

PHP Changelog:

PHP 5.4: Added order constants.