display all images in a directory......

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

display all images in a directory......

Post by dull1554 »

My goal at the end of my current project is to be able to have a acript that will let you upload a file, then the script takes the file creates a thumbnail of it, i want to then have it display all the images in a given directory, and this is where i hit a road block, i can do everything but this, i know that this is not the best place to ask this question but i would really like some help, if anything a suggestion as to where to look to find a answer, i've allready looked at the php manual and i've googled it dead......any help would be greatly appreciated.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
function menu()
{
$folder = "content";
if($handle = opendir($folder))
{
while($files = readdir($handle))
{
if ($files != "." 
&& $files != ".."
&& $files != "Log Out.inc.php"
&& $files != "favelink.inc.php"
)
{
$files = ucwords($files);
$files = substr($files,0,-8);
$showfile = eregi_replace("_"," ",$files);
echo "<a href="main.php?module=$folder&load=$files" title="$showfile">".$showfile."<br />";
}
}
}
}
?>
the above code will read a dir and get the names of the files from it, you can filter any files u dont want to show in there too, i could edit it for you but where is the fun for you in that? :P

have fun.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

thanks that gives me the start that i need, and it is unquestionable that i will have a blast.....i love making money.....lol......thanks
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

could you possiably go a bit farther to explain what does what? that would be appreciated, i dont want to just take it and use your code, i want to be able to understand it and be able to apply it in other applications
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Code: Select all

function menu()
{
$folder = "content";  //folder name
if($handle = opendir($folder))  //open the folder
{
while($files = readdir($handle))  // $files will loop through every file in the directory
{
if ($files != "." && $files != "..")  // readdir returns "." and ".." but they arent' actual files, so strip them out
{
$files = ucwords($files);  //add uppercase to the first letter of all files
$files = substr($files,0,-8);  //strip off all but the first 8 chars of the file
$showfile = eregi_replace("_"," ",$files);  //replace all spaces with "-"'s
echo "<a href="main.php?module=$folder&load=$files" title="$showfile">".$showfile."<br />";  //output a link to the file
}
}
}
}
Here is how PHP.net's example, they say that while($files = readdir($handle)) is the wrong way to loop over the directory:

Code: Select all

<?php
$folder = "/path/to/files";
if ($handle = opendir('$folder')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
           echo "$file\n";
       }
   }
   closedir($handle);
}
?>
This will loop through all the files and show their filename. Just edit it to however you want it.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

so that will output a link to a single file right, is there anyway i can add all the files to a array and then display all the files/images in the array?, and thanks for commenting it out
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

echo is in a loop, so it will display all files
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

oh really ok thankyou
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i ended up using this

Code: Select all

<?php

$folder = "images/";  //folder name
if($handle = opendir($folder))  //open the folder
{
while($files = readdir($handle))  // $files will loop through every file in the directory
{
if ($files != "." && $files != "..")  // readdir returns "." and ".." but they arent' actual files, so strip them out
{
echo "<center><img src=".$folder.$files."></center><br>";

}
}
}

?>
i changed it so rather then echo-ing a link it shows the images in the folder.
thanks for all the help
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

As stated in the PHP.net manual, you should use

Code: Select all

while (false !== ($files = readdir($handle))) &#123;
instead of

Code: Select all

while($files = readdir($handle))
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i made the changes, it works just how i want it to, i have one more question for anywho who may want to answer it. my question is can i make it so if there are more then say 10 images in the folder it will display the first ten images on the first page and add a link to a second or third or whatever page? just so the page is not a mile long, its not needed but theres no doubt in my mind that this feature would be nice.....ass usual any help would be greatly appreciated....
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$max_per_page=10;
$start_from=$_GET['start_from'];
$folder = "images/";  //folder name 
if($handle = opendir($folder))  //open the folder 
{ 
     $i=0;
    while($files = readdir($handle))  // $files will loop through every file in the directory 
  { 
     if ($files != "." && $files != "..")  // readdir returns "." and ".." but they arent' actual files, so strip them out 
    { 
         if($i<$start_from) continue;
         elseif ($i<$start_from+$max_per_page) break;
         else echo "<center><img src=".$folder.$files."></center><br>"; 
    } 
  } 
} 
echo "<a href='{$_SERVER['PHP_SELF']}?start_from=".$start_from-$max_per_page."'>Prev</a><a href='{$_SERVER['PHP_SELF']}?start_from=".$start_from+$max_per_page."'>Next</a>";
Something like this....
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

so like this?

Code: Select all

<?php
echo "<center><h1>Pictures</h1></center>";
$folder = "images/";  //folder name
if($handle = opendir($folder))
{
//while($files = readdir($handle))
while (false !== ($files = readdir($handle)))
{
if ($files != "." && $files != ".." && $files != "Thumbs.db")
{
$max_per_page=10;
$start_from=$_GET['start_from'];
$folder = "images/";  //folder name
if($handle = opendir($folder))  //open the folder
{
     $i=0;
    while($files = readdir($handle))  // $files will loop through every file in the directory
  {
     if ($files != "." && $files != "..")  // readdir returns "." and ".." but they arent' actual files, so strip them out
    {
         if($i<$start_from) continue;
         elseif ($i<$start_from+$max_per_page) break;
         else echo "<center><img src=".$folder.$files."></center><br>";
    }
  }
}
echo "<a href='{$_SERVER['PHP_SELF']}?start_from=".$start_from-$max_per_page."'>Prev</a><a href='{$_SERVER['PHP_SELF']}?start_from=".$start_from+$max_per_page."'>Next</a>"; 

}
}
}

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Look once more at what you had posted. ;)
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

The DuFF has spoken 8) anyway here's something that'll boost you a little to where you want to go:

Code: Select all

<?php

$directory = "/home/virtual/site95/fst/var/www/html/gallery/images/";
$original = $_SERVER['QUERY_STRING'];
$original = $image_path . $original;

$new_width = "160";
$new_height = "125";

$extension = explode(".", $original);
$extension = $extension[1];

   if($extension == "jpg" || $extension == "jpeg") {
      $img = imagecreatefromjpeg($original);
   } elseif($extension == "png") {
      $img = imagecreatefrompng($original);
   }

$width = imagesx($img);
$height = imagesy($img);

$temp_img = imagecreatetruecolor($new_width, $new_height);
$action = imagecopyresized($temp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$action = imagedestroy($img);
$img = $temp_img;

header("Content-type: image/jpeg");
imagejpeg($img);

?>
Errm, it's a resizer I made a few months back. mMm, there's a bunch of things lying around my /tests folder <_<. Btw, you use this a different file and do a this_file.php?nay_rocks.jpg.

-Nay
Post Reply