PHP scandir() Function
In this chapter you will learn:
- Definition for PHP scandir() Function
- Syntax for PHP scandir() Function
- Parameter for PHP scandir() Function
- Return for PHP scandir() Function
- Example - returns an array of all files and directories
Definition
The scandir() function returns an array of files and directories of the specified directory.
Syntax
PHP scandir() Function has the following syntax.
scandir(directory,sorting_order,context);
Parameter
Parameter | Is Required | Description |
---|---|---|
directory | Required. | Directory to be scanned |
sorting_order | Optional. | Sorting order. Default sort order is alphabetical in ascending order (0). Set to SCANDIR_SORT_DESCENDING or 1 to sort in alphabetical descending order, or SCANDIR_SORT_NONE to return the result unsorted |
context | Optional. | Context of the directory handle. |
Return
Returns an array of files and directories on success. FALSE on failure.
Throws an error of level E_WARNING if directory is not a directory
Example
The scandir() function returns an array of all files and directories. If the second parameter is set to 1, scandir() function will sort the array returned reverse alphabetically.
If it is not set, the array is returned sorted alphabetically.
<?PHP//from j a v a 2 s.com
$files = scandir(".", 1);
var_dump($files);
// Sort in ascending order - this is default
$a = scandir(".");
print_r($a);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP set_file_buffer() Function
- Syntax for PHP set_file_buffer() Function
- Parameter for PHP set_file_buffer() Function
- Return for PHP set_file_buffer() Function
- Example - Create an unbuffered stream
Home » PHP Tutorial » PHP File Functions