Having trouble grabbing an image from directory..
I want to display a thumbnail along with a title as an intro/link to a picture album.

I just want to grab an image from the direcory of images that populates the album.

I thought i've done this before... maybe my logic is off tonight!
!- my brain is fried -! this should be easy... maybe you can help.

$dir = "/home/dblsports/www/images/user_uploaded/" . $pictures['directory'];
if ($handle = opendir($dir)) {
	$file = readdir($handle); 
	closedir($handle);
}
$img1 = $file[0];
// Print thumbnail 
echo "	<td><center><a href=\"http://www.dblsports.com/images/display_thumbnails.php?PIC=".$pictures['id']."&directory=".$pictures['directory']."\"><font size=\"2\" face=\"Tahoma\"><b> ".$pictures['title']."</b></font><br>
<img src=\"$img1\" heigth=\"100\" width=\"100\" border=\"0\"></a></center></td>\n";

How can I select an image -on the fly- from a directory?

Thanks,
Phence

    <?php
    
    $dir = "/home/dblsports/www/images/user_uploaded/".$pictures['directory'];
    
    $d = dir($dir);
    
    while(FALSE !=($entry=$d->read()))
    {
      if($entry!='.' && $entry!='..')
      {
        echo echo "    <td><center><a href=\"http://www.dblsports.com/images/display_thumbnails.php?PIC=".$pictures['id']."&directory=".$pictures['directory']."\"><font size=\"2\" face=\"Tahoma\"><b> ".$pictures['title']."</b></font><br>
    <img src=\"".$entry."\" heigth=\"100\" width=\"100\" border=\"0\"></a></center></td>\n";
      }
    }
    
    ?>

    Something like that?

    ~Brett

      nah... that just repeated the image and the title as many times as there were the amount of pictures in the directory.

      and the pictures still doesn't show up. [x] <--- still looks like that.

      your footer inspired me to draw 🙂.

      Thanks for taking taking a hack.

        Oh!!! OMG!!! I'm a moron!!!

        How about the path to the image?!?!?! I'm an IDIOT!!!

        Use this:

        echo "    <td><center><a href=\"http://www.dblsports.com/images/display_thumbnails.php?PIC=".$pictures['id']."&directory=".$pictures['directory']."\"><font size=\"2\" face=\"Tahoma\"><b> ".$pictures['title']."</b></font><br>
        <img src=\"images/user_uploaded/".$pictures['directory'].$img1."\" heigth=\"100\" width=\"100\" border=\"0\"></a></center></td>\n";

        Crapola!!!

        ~Brett

          no... this code...

          $dir = "/home/dblsports/www/images/user_uploaded/".$pictures['directory']; 
          $d = dir($dir); 
          while(FALSE !=($entry=$d->read())){ 
          	if($entry!='.' && $entry!='..'){ 
          		echo "<td><center><a href=\"http://www.dblsports.com/images/display_thumbnails.php?PIC=".$pictures['id']."&directory=".$pictures['directory']."\"><font size=\"2\" face=\"Tahoma\"><b> ".$pictures['title']."</b></font><br> 
          		<img src=\"images/user_uploaded/".$pictures['directory'].$img1."\" heigth=\"100\" width=\"100\" border=\"0\"></a></center></td>\n"; 
          	} 
          }						
          

          ...is showing [x] [x] [x] [x] [x] [x] [x] all accross the page.

          I just wanna see one image that shows up properly. I want to read the directory and just grab one image and display it.

          Thanks so far,
          phence

            Read my above post... you need a path to the image. No path, means no image!!

            ~Brett

              i understand that... i have a path.. i've made that change...

              the problem is $img1 is echoing '.'

              and not the name (ex: lion.jpg) of an image..

              this is the case in both my example and yours...

              Thanks,
              Phence

                umm... that's because the first two items that are returned are directory links to "current directory" (.) and "up one directory" (..). So by using the code below, you can circumvent that problem:

                $img = aray();
                while(FALSE !== ($file=readdir($handle)))
                {
                  if($file != '.' && $file != '..')
                  {
                    array_push($img, $file);
                  }
                }
                $img1 = $img[0];

                The code above came straight from the PHP manual...
                [man]readdir/man :: Example 2

                That's what I'd do.

                ~Brett

                  Maybe i didn't construct this right, but here's what i got...

                  $dir = "/home/dblsports/www/images/user_uploaded/".$pictures['directory']; 
                  $d = dir($dir); 
                  $img = array(); 
                  while(FALSE !== ($file=readdir($d))){ 
                    if($file != '.' && $file != '..'){ 
                  	array_push($img, $file); 
                    } 
                  } 
                  $img1 = $img[0];
                  echo "img1: " . img1 . "<br>";
                  
                  	echo "<td><center><a href=\"http://www.dblsports.com/images/display_thumbnails.php?PIC=".$pictures['id']."&directory=".$pictures['directory']."\"><font size=\"2\" face=\"Tahoma\"><b> ".$pictures['title']."</b></font><br> 
                  	<img src=\"images/user_uploaded/".$pictures['directory']."/".$img1."\" heigth=\"100\" width=\"100\" border=\"0\"></a></center></td>\n"; 
                  
                  

                  it's still not correct..

                  $img1 is not outputing anything. And i am getting a "Warning: readdir(): supplied argument is not a valid Directory resource in /home/www/dblsports/page.php on line 122"

                  line 122 is: while(FALSE !== ($file=readdir($d))){

                  I am lost now.. with first peice of code i tried to contruct it from what i read in the php manual.

                  I used the manual to create the page that displays all of the images... that script works fine.... but i can't seem to figure out how to read and then select one single image from the directory.

                  Thanks for standing by,
                  phence

                    If you're using the object oriented approach ([man]dir/man), then replace "readdir($d)" with "$d->read()".

                    ~Brett

                      That did it. Thanks for sticking by & through my ignorance.

                      Phence

                        Great... glad to hear it.

                        ~Brett

                          Write a Reply...