Jump to content

Simple Photo gallery


tex1820

Recommended Posts

Hi all, my name is tex. I'm new to php. I started to write my first real self made php script however im starting to run into problums. here is what I have so far. The top part of the code came from this website

 

http://snipplr.com/view/4147/count-number-of-files-in-a-directory/

 

I am using it to create a simple web gallery. here is what I have so far:

 

<?
      $dir = "gallery";
      $count = 0;
      if(is_dir($dir)) {
      if($handle = opendir($dir)) {
      while(($file = readdir($handle)) !== false) {
      $count++;
      }
      closedir($handle);
      }
      }
      echo $count;
      
  while ($count > '0')
  {
  	?>
        <a href="gallery/<?php echo $count; ?>.gif"><img src="gallery/<?php echo $count; ?>.gif"></a>
        <? 
	$count--;
	}
	?>

 

Now the thing is, it is finding the images but it says there are 5 images, however there are only 3 in the dir. here is another example:

http://monty.triplethreatairsoft.com/gallerymessedexample.php

 

I left the number to show that it is seeing 5 files. Also why will the images not load? I have a shot in the dark, the php stops loading the images when it loops. am I right about that? how can i fix it? thanks everyone, when it is 100% done, I will be shour to share it :)

 

Tex

Link to comment
Share on other sites

when you are reading the list of files in a directory (either with readdir() or scandir()) you will always get 2 'extra' entries. They are '.' and '..'

. => Current directory

.. => Parent directory

 

you need to add logic in your code to skip these entries. i usually skip anything that starts with a period since (for the most part) anything that starts with a period should be a system/hidden/etc file

 

<?php //I would recommend using full PHP tags as not all PHP installs will have short tags enabled
      //Also, try getting in the habit of indenting...it's much easier to read
      $dir = "gallery";
      $count = 0;
      if(is_dir($dir)) {
            if($handle = opendir($dir)) {
                  while(($file = readdir($handle)) !== false) {
                        if($file{0} == '.') continue; //This line skips files that start with a period
                        echo "<a href=\"{$gallery}/{$file}\"><img src=\"{$gallery}/{$file}\"></a>\n";
                  }
                  closedir($handle);
            }
      }
      echo $count;
?>

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.