Ne.OnZ Posted May 20, 2008 Share Posted May 20, 2008 Hello, I have a simple question. I have this folder with images in them. I was wondering how would I check that folder and the contents inside and echo all the images in them. I'm not looking for actual code for this to be done, but more of a guide, since I learn better by doing it myself. I'm thinking I might need a loop to check the images in the folder. But I have no idea what the actual code is to check the folder and the files in it. I saw on php.net about is_dir, but that only checks if it exists. Not read the contents inside and display. Hope someone can point me to a guide. Thank You! Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/ Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 scandir() will provide a list of files inside the dir 1) Make sure to check for and ignore . and .. as they will show up in the scandir results (i usually just ignore anothing that starts with a period) 2) To see if it's a valid image, you can try getimagesize()...this will require GD (which is most likely installed already) Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545423 Share on other sites More sharing options...
Ne.OnZ Posted May 20, 2008 Author Share Posted May 20, 2008 I'm sorry, but when you say check for and ignore the periods. How would I do that? So I'm assuming when I do scandir(), I put the path inside the parenthesis? Also how would this display the images, you said it would only give a list of the files inside. Sorry if I'm being frustrating to you. Thank You! Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545428 Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 you said you didn't want any actual code <?php $dir = 'images/'; foreach($scandir($dir) as $file){ if($file{0} == '.') continue; //Ignore hidden files print "<img src=\"{$dir}{$file}\" /><br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545431 Share on other sites More sharing options...
Ne.OnZ Posted May 20, 2008 Author Share Posted May 20, 2008 That is exactly why I didn't want it, a lot of syntax things in there I don't understand . For example: ($file{0} == '.'): Why is there curly braces around the 0 and what exactly does that 0 mean. Thank You again! Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545433 Share on other sites More sharing options...
The Little Guy Posted May 20, 2008 Share Posted May 20, 2008 or foreach(glob('images/*.jpg') as $file){ echo $file.'<br>'; } Glob doesn't grab hidden files, and you can limit the files you want, such as jpg, gif, png etc. Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545453 Share on other sites More sharing options...
Jeremysr Posted May 20, 2008 Share Posted May 20, 2008 That is exactly why I didn't want it, a lot of syntax things in there I don't understand . For example: ($file{0} == '.'): Why is there curly braces around the 0 and what exactly does that 0 mean. Thank You again! I've never seen curly braces used like that, I always use $file[0] with square brackets. Anyways, that just means "the 0th character of the $file string." A string is an array of single characters, and the number in square brackets says which character in the string to return. So ($file[0] == '.') returns true if $file begins with a period. Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545454 Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 I've always uesed {0}, but [0] will work too. Here is info on that: http://us.php.net/manual/en/language.types.string.php#language.types.string.substr -you pass the path of a directory to scandir and it will return an array of files -using foreach, we can loop over the list of files. each time the loop runs, the next item in the array will be stored in $file -$file{0} or $file[0] will give you the first character of the filename. if it's a period, you will want to skip it. (continue just tells the loop to ignore the code in the rest of the loop, go back to the top, and go to the next item) Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545542 Share on other sites More sharing options...
PFMaBiSmAd Posted May 20, 2008 Share Posted May 20, 2008 The {} syntax to access elements of a string is removed in php6. Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545563 Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 The {} syntax to access elements of a string is removed in php6. um...eh...like I said...use [] Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545594 Share on other sites More sharing options...
Ne.OnZ Posted May 20, 2008 Author Share Posted May 20, 2008 I tried your code and I also tried glob, but I didn't get the results I wanted. $dir = 'board/images/gallery/'; foreach($scandir($dir) as $file) { if($file[0] == '.') continue; print "<img src=\"{$dir}{$file}\" />"; } I got an error saying: Fatal error: Call to undefined function: () in /home/divnxn5/public_html/account/avatar.php on line 45. Line 45 is the foreach statement there. So I'm guessing the $scandir? I tried the glob, but that only printed the the names of the images, not the actual images. Thank You again! Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545760 Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 scandir() not $scandir()....sorry Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545764 Share on other sites More sharing options...
Ne.OnZ Posted May 20, 2008 Author Share Posted May 20, 2008 I get the almost the same error: Fatal error: Call to undefined function: scandir() in /home/divnxn5/public_html/account/avatar.php on line 45. Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545777 Share on other sites More sharing options...
BlueSkyIS Posted May 20, 2008 Share Posted May 20, 2008 scandir requires PHP 5. Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545780 Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 ug...people are still using PHP4...UPGRADE PEOPLE Example #2 on this page has an alternative to scandir for PHP4: http://us2.php.net/scandir Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545785 Share on other sites More sharing options...
Ne.OnZ Posted May 20, 2008 Author Share Posted May 20, 2008 Sorry, I don't own my own server. My host still has php4. I asked to upgrade but they said they wouldn't until php 6 is out. I'll read that link. Thank You. Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545790 Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 wow...sounds like it's time for a new host i have 1&1, which has PHP4 by default, but one line in an htaccess file enables PHP5 Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545792 Share on other sites More sharing options...
prcollin Posted May 20, 2008 Share Posted May 20, 2008 wow...sounds like it's time for a new host i have 1&1, which has PHP4 by default, but one line in an htaccess file enables PHP5 I own a hosting company with php5 if anyone needs hosting cheap, just let me know. It has Apache 2.2 and mysql's latest version also. It uses cpanel and has fantastico too if you need it at all.! So we dont have problems anymore with php issue Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545794 Share on other sites More sharing options...
Ne.OnZ Posted May 20, 2008 Author Share Posted May 20, 2008 I heard bad reviews on 1&1 o.o. ??? ??? I own a hosting company with php5 if anyone needs hosting cheap, just let me know. It has Apache 2.2 and mysql's latest version also. It uses cpanel and has fantastico too if you need it at all.! So we dont have problems anymore with php issue Smiley Can I see a website for your comapny? o.o Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545796 Share on other sites More sharing options...
rhodesa Posted May 20, 2008 Share Posted May 20, 2008 What kind of bad stuff have you heard about 1&1? The only problem I have had with them is some occasional late night downtime. But I have the developer package, so don't expect clustered failover. I real like the fact that I can have many domains, and don't have to share email addresses. Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545804 Share on other sites More sharing options...
The Little Guy Posted May 20, 2008 Share Posted May 20, 2008 The reason glob didn't work like you wanted, is because you I didn't tell it to display as images, I only told it to display as text. To display them as images use this: foreach(glob('images/*.jpg') as $file){ echo '<img src="'.$file.'" alt="JPG IMAGE" /><br>'; } Link to comment https://forums.phpfreaks.com/topic/106409-check-folders-contents-and-display/#findComment-545868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.