$directoryPath = '/home/user/projects/using-glob';
$fileExtension = 'jpg';
// View files with the specified extension
foreach (glob($directoryPath . '/*.' . $fileExtension) as $image) {
echo basename($image) . "\n";
}
// View all files without a specified extension
foreach (glob($directoryPath . '/*') as $file) {
echo basename($file) . "\n";
}
code
File Listing with glob()
This PHP script utilizes the glob() function to list files in a specified directory with a particular extension and all files without a specified extension. The comments provide explanations for each part of the code.
File listing
Directory path
File extension
glob()