PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
In PHP, readdir()
and scandir()
are two functions that allow you to read directories and iterate over the files and folders they contain.
readdir()
The readdir()
function reads an entry from the directory handle resource (which is typically created using opendir()
). Here's how you can use it:
<?php $dir = opendir('/path/to/your/directory'); if ($dir) { while (($file = readdir($dir)) !== false) { echo 'filename: ' . $file . '<br />'; } closedir($dir); } ?>
In this example, opendir()
is used to open a directory. Then, readdir()
is used in a loop to read the contents of the directory. Finally, closedir()
is used to close the directory handle.
scandir()
The scandir()
function returns an array of files and directories from the directory. Here's an example:
<?php $dir = '/path/to/your/directory'; if (is_dir($dir)) { $files = scandir($dir); foreach ($files as $file) { echo 'filename: ' . $file . '<br />'; } } ?>
In this example, scandir()
is used to retrieve the contents of the directory. The result is an array, which is then iterated over using a foreach
loop.
Note:
Both readdir()
and scandir()
will include the special entries .
and ..
in their results. These represent the current directory and the parent directory, respectively. If you want to exclude these entries, you will need to do so manually in your code.
For example:
<?php $dir = '/path/to/your/directory'; if (is_dir($dir)) { $files = scandir($dir); foreach ($files as $file) { if ($file != "." && $file != "..") { echo 'filename: ' . $file . '<br />'; } } } ?>
In this modified scandir()
example, the if
statement excludes .
and ..
from the output.
Iterating through files with PHP readdir()
:
Use readdir()
to iterate through files in a directory:
<?php $dir = 'path/to/directory'; if (is_dir($dir)) { if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { echo "File: $file\n"; } } closedir($handle); } }
PHP readdir()
for reading directories:
Read directories using readdir()
:
<?php $dir = 'path/to/directory'; if (is_dir($dir)) { if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != '.' && $entry != '..' && is_dir($dir . '/' . $entry)) { echo "Subdirectory: $entry\n"; } } closedir($handle); } }
Handling errors during file reading with readdir()
in PHP:
Handle errors during file reading:
<?php $dir = 'path/to/directory'; if (is_dir($dir)) { if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { echo "File: $file\n"; } } closedir($handle); } else { echo "Error opening directory"; } } else { echo "Not a valid directory"; }
PHP readdir()
vs scandir()
:
Understand the difference between readdir()
and scandir()
:
<?php // Using readdir() $dir = 'path/to/directory'; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { // Process files } closedir($handle); } // Using scandir() $dir = 'path/to/directory'; $files = scandir($dir); foreach ($files as $file) { // Process files }
Recursive directory reading with PHP readdir()
:
Recursively read directories using readdir()
:
<?php function readDirectory($dir) { if (is_dir($dir)) { if ($handle = opendir($dir)) { while (false !== ($entry = readdir($handle))) { if ($entry != '.' && $entry != '..') { if (is_dir($dir . '/' . $entry)) { echo "Subdirectory: $entry\n"; readDirectory($dir . '/' . $entry); } else { echo "File: $entry\n"; } } } closedir($handle); } } } $startDir = 'path/to/starting/directory'; readDirectory($startDir);
PHP opendir()
, readdir()
, and closedir()
combination:
Combine opendir()
, readdir()
, and closedir()
for directory reading:
<?php $dir = 'path/to/directory'; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { echo "File: $file\n"; } } closedir($handle); }
Efficient file filtering with readdir()
in PHP:
Efficiently filter files using readdir()
:
<?php $dir = 'path/to/directory'; $allowedExtensions = ['txt', 'pdf', 'doc']; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { $extension = pathinfo($file, PATHINFO_EXTENSION); if ($file != '.' && $file != '..' && in_array($extension, $allowedExtensions)) { echo "Allowed File: $file\n"; } } closedir($handle); }
PHP readdir()
and file sorting options:
Read and sort files using readdir()
:
<?php $dir = 'path/to/directory'; $files = []; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { $files[] = $file; } } closedir($handle); } // Sort files sort($files); foreach ($files as $file) { echo "File: $file\n"; }
Reading hidden files with PHP readdir()
:
Read hidden files using readdir()
:
<?php $dir = 'path/to/directory'; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..' && strpos($file, '.') === 0) { echo "Hidden File: $file\n"; } } closedir($handle); }
PHP scandir()
example:
Use scandir()
to list files and directories:
<?php $dir = 'path/to/directory'; $files = scandir($dir); foreach ($files as $file) { if ($file != '.' && $file != '..') { echo "Entry: $file\n"; } }
Listing files and directories with PHP scandir()
:
List files and directories using scandir()
:
<?php $dir = 'path/to/directory'; $entries = scandir($dir); foreach ($entries as $entry) { if ($entry != '.' && $entry != '..') { echo "Entry: $entry\n"; } }
PHP scandir()
for sorting directory contents:
Use scandir()
for sorting directory contents:
<?php $dir = 'path/to/directory'; $entries = scandir($dir); // Remove '.' and '..' entries $entries = array_diff($entries, ['.', '..']); // Sort entries sort($entries); foreach ($entries as $entry) { echo "Entry: $entry\n"; }
Filtering files with PHP scandir()
based on file type:
Filter files based on file type using scandir()
:
<?php $dir = 'path/to/directory'; $entries = scandir($dir); foreach ($entries as $entry) { if ($entry != '.' && $entry != '..' && is_file($dir . '/' . $entry)) { echo "File: $entry\n"; } }
Recursive directory listing with PHP scandir()
:
Recursively list directories using scandir()
:
<?php function recursiveScanDir($dir) { $entries = scandir($dir); foreach ($entries as $entry) { if ($entry != '.' && $entry != '..') { echo "Entry: $entry\n"; if (is_dir($dir . '/' . $entry)) { recursiveScanDir($dir . '/' . $entry); } } } } $startDir = 'path/to/starting/directory'; recursiveScanDir($startDir);
Combining opendir()
, readdir()
, and scandir()
for comprehensive directory handling in PHP:
Combine various directory handling functions for comprehensive processing:
<?php $dir = 'path/to/directory'; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { echo "File: $file\n"; } } closedir($handle); } // Using scandir() $entries = scandir($dir); foreach ($entries as $entry) { if ($entry != '.' && $entry != '..' && is_dir($dir . '/' . $entry)) { echo "Subdirectory: $entry\n"; } }
PHP readdir()
and scandir()
for dynamic directory handling:
Use readdir()
and scandir()
dynamically based on conditions:
<?php $dir = 'path/to/directory'; if (someCondition()) { // Use readdir() if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { // Process files } closedir($handle); } } else { // Use scandir() $entries = scandir($dir); foreach ($entries as $entry) { // Process entries } }