What function can be used to read all images from a folder in PHP?
To read all images from a folder in PHP, you can use the `scandir()` function to get a list of all files in the directory, and then filter out only the image files based on their file extensions. You can use functions like `pathinfo()` to check the file extension and determine if it is an image file.
$folder = 'path/to/folder';
$files = scandir($folder);
foreach ($files as $file) {
$fileInfo = pathinfo($file);
$extension = strtolower($fileInfo['extension']);
if (in_array($extension, ['jpg', 'jpeg', 'png', 'gif'])) {
echo '<img src="' . $folder . '/' . $file . '" alt="' . $file . '">';
}
}
Related questions
-
SQL injection prepared statements PDO
-
email address invalid error
-
PHP dynamic menus menu creation