Jump to content

Check folder's Contents and Display


Ne.OnZ

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

That is exactly why I didn't want it, a lot of syntax things in there I don't understand  :P.

 

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
Share on other sites

That is exactly why I didn't want it, a lot of syntax things in there I don't understand  :P.

 

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
Share on other sites

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
Share on other sites

I tried your code and I also tried glob, but I didn't get the results I wanted.  :o

 

$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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.