kryppienation Posted July 8, 2009 Share Posted July 8, 2009 I am having trouble viewing images, i've looked up a simple script online and i'm just trying to view all the images i have in a current folder. I have the viewpics.php file in the folder containing the folder with the images. Here is my code, does anyone know why there are no images showing up when i do this? All of the images are in the "pengame5-22-2009" folder, which is in the folder that this php file is in. <?php $path='pengame5-22-2009/'; $handle=opendir($path); while (($file = readdir($handle))!==false) { if(strlen($file>3)){echo "<img src=$path$file> <br>";} } closedir($handle); ?> Link to comment https://forums.phpfreaks.com/topic/165280-solved-viewing-images-problem/ Share on other sites More sharing options...
thebadbad Posted July 9, 2009 Share Posted July 9, 2009 Weird syntax you've got going there. scandir() or glob() is much easier to use (if you're using PHP 5, that is): <?php $files = scandir('pengame5-22-2009/'); foreach ($files as $path) { echo "<img src=\"$path\" /><br />"; } ?> If the folder contains other files than the images you want to show, you'll have to filter them with e.g. glob('pengame5-22-2009/*.jpg'). Link to comment https://forums.phpfreaks.com/topic/165280-solved-viewing-images-problem/#findComment-871598 Share on other sites More sharing options...
kryppienation Posted July 9, 2009 Author Share Posted July 9, 2009 Weird syntax you've got going there. scandir() or glob() is much easier to use (if you're using PHP 5, that is): <?php $files = scandir('pengame5-22-2009/'); foreach ($files as $path) { echo "<img src=\"$path\" /><br />"; } ?> If the folder contains other files than the images you want to show, you'll have to filter them with e.g. glob('pengame5-22-2009/*.jpg'). Ok, i have tried to use this code, and i've gotten at least better results than the first time... i got no errors or nothing on the first script, just a blank white page. With this code it tried to load the images i guess but all of them are broken and none of them actually showed an image. This is the outpud of the source from the broken images. http://www.homickshouse.com/bobby/bobby%20100.jpg The following is what the output needs to actually be for the image to show up. http://www.homickshouse.com/bobby/pengame5-22-2009/bobby%20100.jpg if you would like to see the live results you can see those at http://www.homickshouse.com/bobby/viewpics.php and also for some reason it is trying to access the main directory of my http because it's asking for a username and password and that would only happen when i try to access my root directory. Link to comment https://forums.phpfreaks.com/topic/165280-solved-viewing-images-problem/#findComment-871610 Share on other sites More sharing options...
kryppienation Posted July 9, 2009 Author Share Posted July 9, 2009 Weird syntax you've got going there. scandir() or glob() is much easier to use (if you're using PHP 5, that is): <?php $files = scandir('pengame5-22-2009/'); foreach ($files as $path) { echo "<img src=\"$path\" /><br />"; } ?> If the folder contains other files than the images you want to show, you'll have to filter them with e.g. glob('pengame5-22-2009/*.jpg'). Ok, i have tried to use this code, and i've gotten at least better results than the first time... i got no errors or nothing on the first script, just a blank white page. With this code it tried to load the images i guess but all of them are broken and none of them actually showed an image. This is the outpud of the source from the broken images. http://www.homickshouse.com/bobby/bobby%20100.jpg The following is what the output needs to actually be for the image to show up. http://www.homickshouse.com/bobby/pengame5-22-2009/bobby%20100.jpg if you would like to see the live results you can see those at http://www.homickshouse.com/bobby/viewpics.php and also for some reason it is trying to access the main directory of my http because it's asking for a username and password and that would only happen when i try to access my root directory. Well i have modified this code to show the pictures now, the only problem is that it is trying to create some kinda pictures at the beginning that are not really there... It's calling something out wrong i think. Does anyone know why this is happening? The code i used to view the pictures correctly is: <?php $files = scandir('pengame5-22-2009/'); foreach ($files as $path) { echo "<img src=\"pengame5-22-2009/$path\" /><br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/165280-solved-viewing-images-problem/#findComment-871649 Share on other sites More sharing options...
thebadbad Posted July 9, 2009 Share Posted July 9, 2009 My bad, it's because the filenames and not the full paths are returned. And the other problem you're having occurs because "." and ".." also are included in the list. You'll need to remove "." and ".." from the $files array: <?php $path = 'pengame5-22-2009/'; $files = scandir($path); //unset "." and ".." unset($files[0], $files[1]); foreach ($files as $filename) { echo "<img src=\"$path$filename\" /><br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/165280-solved-viewing-images-problem/#findComment-871859 Share on other sites More sharing options...
kryppienation Posted July 9, 2009 Author Share Posted July 9, 2009 My bad, it's because the filenames and not the full paths are returned. And the other problem you're having occurs because "." and ".." also are included in the list. You'll need to remove "." and ".." from the $files array: <?php $path = 'pengame5-22-2009/'; $files = scandir($path); //unset "." and ".." unset($files[0], $files[1]); foreach ($files as $filename) { echo "<img src=\"$path$filename\" /><br />"; } ?> Thank you so much, that worked perfectly. Much appriciated!!! Link to comment https://forums.phpfreaks.com/topic/165280-solved-viewing-images-problem/#findComment-872326 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.