How can the pathinfo function be utilized in PHP to determine the file extension of images in a directory?
To determine the file extension of images in a directory using the pathinfo function in PHP, you can loop through the files in the directory, use pathinfo to extract the extension of each file, and then check if the extension is one commonly used for images (e.g., jpg, png, gif). This can be useful for filtering out non-image files in a directory.
$directory = "/path/to/directory/";
$allowedExtensions = array("jpg", "jpeg", "png", "gif");
$files = scandir($directory);
foreach ($files as $file) {
if (is_file($directory . $file)) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if (in_array($extension, $allowedExtensions)) {
echo "Image file found: " . $file . "<br>";
}
}
}