The dir class
<?php //Syntax dir(string $directory, ?resource $context = null ): Directory|false
Since PHP 3, PHP comes with a built-in class that offers several methods to access the file system. It is possible to get a list of all entries in a directory.
<?php $dirPath = 'uploads/gif/'; $dh = dir($dirPath); if ($dh === false) exit ('Error: unable to open dir'); while (false !== ($entry = $dh->read())) { echo $entry . '<br>'; } $dh->close();
You instantiate the class without the keyword new
, just by providing the desired path as a parameter. Then the method read()
iterates through the list of entries. The preceding code does exactly this and prints out the contents of the current directory.
. .. abc.gif dog.gif image.gif tamin.gif
Note that the two directory entries .
(current directory) and ..
(parent directory, if you are not in the root directory) are printed out.
scandir()
The scandir()
function list files and directories inside the specified path and returns an array of files and directories from the path/directory.
<?php //Syntax scandir(string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, ?resource $context = null ): array|false
This function accepts three parameters:
$directory
the target directory for scanning$sorting_order
to set sort order, default is alphabetical in ascending order, setSCANDIR_SORT_DESCENDING
for descending order andSCANDIR_SORT_NONE
for unsorted result.context
Example: Using scandir() function
<?php $dirPath = 'uploads/gif/'; $entries = scandir($dirPath); if ($entries === false) exit ("Error: unable to open $dirPath"); foreach ($entries as $entry) { echo $entry .'<br>'; }
The above example output is:
. .. abc.gif dog.gif image.gif tamin.gif
opendir(), readdir() and closedir()
The opendir()
function returns a “directory handle” to the directory named in the function call. The directory handle is then used by the readdir()
function to iterate over the directory, returning a single entry each time it is invoked. Once done, the closedir()
function closes the directory handle.
<?php $dirPath = 'uploads/gif/'; if ( ! is_dir($dirPath) ) exit ("Error: $dirPath is not a directory"); $handle = opendir($dirPath); if ($handle === false) dir ("Error: unable to open $dirPath"); while (($entry = readdir($handle)) !== false) { echo $entry .'<br>'; } closedir($handle);
The above code outputs the following result:
. .. abc.gif dog.gif image.gif tamin.gif
Working with Files in PHP:
- Returning or Downloading Files with an HTTP Request
- Reading a File into a String or Array
- PHP Opening and Closing Files
- Reading files by line or by character
- Writing and appending to files
- Reading and Writing CSV Files
- Parsing INI Files and Strings
- Check File Type (whether a file is a directory or a file)
- Understanding file Permissions in PHP
- Reading Information About Files in PHP
- Copying, Moving and Deleting Files in PHP
- Reading Directories Contents in PHP
- Browse directories recursively
- Zipping and Unzipping a File with Zlib and Bzip2
- Zip and Unzip Archives with ZipArchive
- Using Relative Paths for File Access