Sir Softsand Posted June 8, 2007 Share Posted June 8, 2007 Hi, I got a folder on my FTP called "images" I have a page. I want to show all of the images in the directory image on that page without keep updating that page with HTML <img src="... My friend said you could do this with PHP, is that true? Thanks in advance, Sir Softsand Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/ Share on other sites More sharing options...
wildteen88 Posted June 8, 2007 Share Posted June 8, 2007 Yes that is true. You can use pho's readdir function to read all the files/folders in the images folder. Then using a while loop you can echo out the HTML to display the images on your webpage. Before you take on this project of yours make sure you know the basics of PHP. It will be better for you to learn the basics of PHP, such as variables, functions, loops, arrays, data types, superglobals ($_POST, $_GET etc) before you get started. Also make sure your hosting account has PHP enabled -check with your host. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-270913 Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 <?php $handle = opendir('/images'); while ($file = readdir($handle)) { echo "<img src=\"$file\" alt=\"\" /><br />\n"; } closedir($handle); ?> Untested, I suggest you read up on the functions used though Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-270918 Share on other sites More sharing options...
Sir Softsand Posted June 8, 2007 Author Share Posted June 8, 2007 Sorry, I made a mistake, the dir's name is: avatars This code doesn't work: <?php $handle = opendir('/avatars'); while ($file = readdir($handle)) { echo "<img src=\"$file\" alt=\"\" /><br />\n"; } closedir($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-270924 Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 Any errors? Try CHMODing /avatars to 777. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-270925 Share on other sites More sharing options...
Sir Softsand Posted June 8, 2007 Author Share Posted June 8, 2007 avatars dir is now 77 CHMOD, don't get errors, the images just don't show up. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-270933 Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 View the source and paste the output please. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-270934 Share on other sites More sharing options...
wildteen88 Posted June 8, 2007 Share Posted June 8, 2007 Use the following path: ./avatars/ and not /avatars If you are running it on a UNIX based platform, PHP will treate / as the root (of the hard drive) and so it will try to access a folder called avatars in the root of the hard drive. The root will be most probably out of bounds to you. What you want to do is place a period before the slash (./) this will tell PHP to look in the current directory and not the root. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-270958 Share on other sites More sharing options...
penguin0 Posted June 8, 2007 Share Posted June 8, 2007 $handle = opendir('./images'); while ($file = readdir($handle)) { echo "<img src=\"$file\" alt=\"\" /><br />\n"; } closedir($handle); this works for me, but the images are not comming out. When I click properties on the image, it shows www.mysite.com (IE the site I have them on) and nothing else. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271012 Share on other sites More sharing options...
penguin0 Posted June 8, 2007 Share Posted June 8, 2007 For the image to work it needs to be: $handle = opendir('./images'); while ($file = readdir($handle)) { echo "<img src=\"images/$file\" alt=\"\" /><br />\n"; } closedir($handle); Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271114 Share on other sites More sharing options...
Sir Softsand Posted June 9, 2007 Author Share Posted June 9, 2007 For the image to work it needs to be: $handle = opendir('./images'); while ($file = readdir($handle)) { echo "<img src=\"images/$file\" alt=\"\" /><br />\n"; } closedir($handle); That worked, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271423 Share on other sites More sharing options...
Sir Softsand Posted June 9, 2007 Author Share Posted June 9, 2007 Ok, I know i clicked solved, but now I got a problem. The avatars showed up every time. But I changed host, and just transferred all data from the other host I ad before. The avatars still showed up, but when I refreshed the page, 2 avatars dissapeared, when I refreshed again more avatars dissapeared, then I refreshed again and they were all gone! Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271460 Share on other sites More sharing options...
chigley Posted June 9, 2007 Share Posted June 9, 2007 Have you changed the permissions of /avatars on the new server? Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271462 Share on other sites More sharing options...
Sir Softsand Posted June 9, 2007 Author Share Posted June 9, 2007 Permissions changed while transferring data, going to check. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271463 Share on other sites More sharing options...
Sir Softsand Posted June 9, 2007 Author Share Posted June 9, 2007 I CHMODed the avatars dir back, but they still don't show up. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271465 Share on other sites More sharing options...
Sir Softsand Posted June 10, 2007 Author Share Posted June 10, 2007 They show up again Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271687 Share on other sites More sharing options...
Sir Softsand Posted June 10, 2007 Author Share Posted June 10, 2007 And for some reason, they disappear now again Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271688 Share on other sites More sharing options...
Sir Softsand Posted June 10, 2007 Author Share Posted June 10, 2007 Can anyone help me? Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271785 Share on other sites More sharing options...
AndyB Posted June 10, 2007 Share Posted June 10, 2007 Either give a link to the page where we can view the generated code, or post the generated code here. We really can't tell what might be happening looking at php fragments, the source of your problem is within the generated html page. Appearing and disappearing sounds like you're seeing cached versions. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271791 Share on other sites More sharing options...
Sir Softsand Posted June 10, 2007 Author Share Posted June 10, 2007 Here's the link: http://msncommunity.2kool4u.net/avatars.php Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271794 Share on other sites More sharing options...
AndyB Posted June 10, 2007 Share Posted June 10, 2007 I see a whole series of images. The first two lines below obviously have a problem, the rest are fine. Check your php code to see where the first two lines are coming from to fix them. <br /><img src="avatars/." alt="" / > <img src="avatars/.." alt="" / > <img src="avatars/cirkels1.jpg" alt="" / > ... more images follow Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271798 Share on other sites More sharing options...
Sir Softsand Posted June 10, 2007 Author Share Posted June 10, 2007 Yes, you see all the images, but after a while refresh the page, do it again and again and eventually some will disappear. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271800 Share on other sites More sharing options...
Sir Softsand Posted June 10, 2007 Author Share Posted June 10, 2007 I can see them again now, but it's only a matter of time untill they'll start disappearing again. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271801 Share on other sites More sharing options...
Sir Softsand Posted June 10, 2007 Author Share Posted June 10, 2007 Here's my php code: <?php include('overall_header.html'); $handle = opendir('./avatars'); while ($file = readdir($handle)) { echo "<img src=\"avatars/$file\" alt=\"\" / > \n"; } closedir($handle); include('overall_footer.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271803 Share on other sites More sharing options...
AndyB Posted June 10, 2007 Share Posted June 10, 2007 No. If they appear once and you don't change the code then they'll always appear (unless it's hosted on a dreadfully bad server and you have a really bad internet connection, in which case you might have problems seeing them). The problem with the code you posted is that it doesn't ignore . and .., i.e. folder structure in the folder you're reading. That's where the incorrect html code originates. Take a closer look at user notes on the manual page on readdir - or use glob() instead. Quote Link to comment https://forums.phpfreaks.com/topic/54784-solved-image-page/#findComment-271805 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.