How can we filter out only jpg files when reading files from a directory in PHP?
To filter out only jpg files when reading files from a directory in PHP, we can use the glob() function with a wildcard pattern to match only files with a .jpg extension. This function returns an array of file names that match the specified pattern.
$files = glob('path/to/directory/*.jpg');
foreach($files as $file) {
echo $file . "<br>";
}
Keywords
Related Questions
- What are the best practices for checking the length of a filename in PHP before performing operations like unlink?
- What are common formatting issues when outputting MySQL query results in a table using PHP?
- Is there a more efficient alternative to using output buffering for writing content to a text file in PHP?