vetman Posted April 30, 2008 Share Posted April 30, 2008 I can read a dir of images and display the file names. How can I view the images? Any help would be appreciated. I am new to this! This is my code: <?php //Open images directory $dir = dir("../images"); //List files in images directory while (($file = $dir->read()) !== false) { echo "filename: " . $file . "<br />"; } $dir->close(); ?> Link to comment https://forums.phpfreaks.com/topic/103641-i-can-read-a-dir-of-images-and-display-the-file-names-how-can-i-view-the-images/ Share on other sites More sharing options...
dooper3 Posted April 30, 2008 Share Posted April 30, 2008 Just use <img src=... as usual in for HTML image displays. Your code should look like this: <?php //Open images directory $dir = dir("../images"); //List files in images directory while (($file = $dir->read()) !== false) { echo '<img src="$file" alt="my image" width="" height="" />'; "; } $dir->close(); ?> Link to comment https://forums.phpfreaks.com/topic/103641-i-can-read-a-dir-of-images-and-display-the-file-names-how-can-i-view-the-images/#findComment-530709 Share on other sites More sharing options...
vetman Posted April 30, 2008 Author Share Posted April 30, 2008 I modified the code to match yours, all I got was little broken images. So then I put in width , height at 100 and got larger broken images. Not sure what's up! Any suggestions? Thanks Link to comment https://forums.phpfreaks.com/topic/103641-i-can-read-a-dir-of-images-and-display-the-file-names-how-can-i-view-the-images/#findComment-530726 Share on other sites More sharing options...
DarkWater Posted April 30, 2008 Share Posted April 30, 2008 Change: <img src="$file" alt="my image" width="" height="" /> To: <img src="../images/' . $file . '" alt="my image" width="" height="" /> =) Link to comment https://forums.phpfreaks.com/topic/103641-i-can-read-a-dir-of-images-and-display-the-file-names-how-can-i-view-the-images/#findComment-530734 Share on other sites More sharing options...
vetman Posted April 30, 2008 Author Share Posted April 30, 2008 That's it, it works now! I appreciate the help! Thanks Link to comment https://forums.phpfreaks.com/topic/103641-i-can-read-a-dir-of-images-and-display-the-file-names-how-can-i-view-the-images/#findComment-530742 Share on other sites More sharing options...
vetman Posted April 30, 2008 Author Share Posted April 30, 2008 Okay, I got myself into another bind. I want to put 3 files in a row across the page, then 3 in the next line and so forth. This is the modified code below: <?php //Open images directory $dir = dir("../images"); //List files in images directory while (($file = $dir->read()) !== false) { // how many entries per row $per_row = 3; echo ' <table border=1>'; $count = 0; echo '<img src="../images/' . $file . '" alt="my image" width="" height="" />'; for($x = 0; $x < count($file); $x++) { if($count == 0) { echo ' <tr>'; } echo ' <td>'.$file[$x].'</td>'; $count++; if(($count == $per_row) || ($file[$x] == end($file))) { echo ' </tr>'; $count = 0; } } echo ' </table>'; } $dir->close(); ?> Link to comment https://forums.phpfreaks.com/topic/103641-i-can-read-a-dir-of-images-and-display-the-file-names-how-can-i-view-the-images/#findComment-530760 Share on other sites More sharing options...
hitman6003 Posted May 1, 2008 Share Posted May 1, 2008 <?php //Open images directory $dir = dir("../images"); // this shouldn't be in the while loop $per_row = 3; echo ' <table> <tr>'; $i = 0; // create the rows while (($file = $dir->read()) !== false) { $i++; // close and start another row if the correct number has been displayed if ($i % $per_row == 0) { echo ' </tr> <tr>'; } echo ' <td><img src="../images/' . $file . '" alt="my image" /></td>'; } // finish the current row while ($i % $per_row != 0) { echo ' <td> </td>'; } echo ' </tr> </table>'; } $dir->close(); ?> Link to comment https://forums.phpfreaks.com/topic/103641-i-can-read-a-dir-of-images-and-display-the-file-names-how-can-i-view-the-images/#findComment-530767 Share on other sites More sharing options...
vetman Posted May 1, 2008 Author Share Posted May 1, 2008 Thank you, I'm working on reading the manual more, I hear you. I'm trying. Link to comment https://forums.phpfreaks.com/topic/103641-i-can-read-a-dir-of-images-and-display-the-file-names-how-can-i-view-the-images/#findComment-530776 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.