How can readdir be used to display images in a table format in PHP?
To display images in a table format using readdir in PHP, you can first read the files in a directory using readdir, filter out only the image files, and then display them in a table format using HTML. This can be achieved by looping through the files, checking if they are images using functions like getimagesize, and then displaying them in a table format with appropriate HTML tags.
<?php
$dir = "images/";
$files = scandir($dir);
echo "<table>";
foreach($files as $file){
if(is_file($dir . $file) && in_array(pathinfo($file, PATHINFO_EXTENSION), ['jpg', 'jpeg', 'png', 'gif'])){
echo "<tr><td><img src='".$dir.$file."' width='100'></td></tr>";
}
}
echo "</table>";
?>
Keywords
Related Questions
- How can fsockopen() be utilized in PHP to execute scripts on a remote server without triggering a redirect like header()?
- How does the empty() function in PHP handle values like 0, "0", and ""? What should users be aware of when using it?
- How can beginners avoid wasting money on unnecessary PHP resources?