What is the best way to display images stored in a directory using PHP?
To display images stored in a directory using PHP, you can use the `scandir()` function to get a list of files in the directory, filter out only the image files, and then loop through them to display each image using the `<img>` tag.
<?php
$dir = 'path/to/directory';
$files = array_diff(scandir($dir), array('..', '.'));
foreach($files as $file){
if(is_file($dir . '/' . $file)){
echo '<img src="' . $dir . '/' . $file . '" alt="' . $file . '">';
}
}
?>
Related Questions
- How can PHP developers troubleshoot issues with form submission and database queries in PHP applications?
- What are the differences between using htmlentities and htmlspecialchars for escaping characters in PHP?
- What are the best practices for passing arrays as parameters to user-defined functions in PHP?