Showing All Images In Folder

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

Post Reply
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Showing All Images In Folder

Post by CoolAsCarlito »

Okay I want my photos.php file to show all the photos I have in my /kowphotos/ folder on my server. How do I go out having that done?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Showing All Images In Folder

Post by califdon »

CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Showing All Images In Folder

Post by CoolAsCarlito »

The only thing is I don't want my images listed. I actually want them shown.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Showing All Images In Folder

Post by flying_circus »

CoolAsCarlito wrote:The only thing is I don't want my images listed. I actually want them shown.
Pause between firing rounds.

Re-read that link that califdon post and try to think of a clever way to use that returned information to solve your problem.

Code: Select all

<?php
  $path_to_images = 'images/';
  $images = scandir($path_to_images);
 
  foreach($images as $image)
  {
    print '<img src="' . $path_to_images . $image . '" />';
  }
?>
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Showing All Images In Folder

Post by CoolAsCarlito »

Code: Select all

 
<?php
  $path_to_images = 'images/kowphotos';
  $images = scandir($path_to_images);
 
  foreach($images as $image)
  {
    print '<img src="' . $path_to_images . $image . '" />';
  }
?>
 
What am I missing?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Showing All Images In Folder

Post by flying_circus »

CoolAsCarlito wrote:What am I missing?
A forward slash in your $path_to_images variable (at the end).
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Showing All Images In Folder

Post by CoolAsCarlito »

Okay I did that but what does this error mean?

Fatal error: Call to undefined function: scandir() in /home/content/y/a/n/yankeefaninkc/html/kowphotos2.php on line 3
jazzfan
Forum Newbie
Posts: 1
Joined: Tue Aug 12, 2008 4:19 pm

Re: Showing All Images In Folder

Post by jazzfan »

that means you don't have php 5...
you can use system directive, and you should make a simple recruisive algorithm to scan subfolders....
this is solution for linux/unix operatin systems....

Code: Select all

 
function rek ($path) {
     $output = system ("ls -1 $path", $status);
 
     //if failed to run above command (probebly premission denied)
     if ($status != 0) return;
     
     $ls = split ("\n", $output);
     foreach ($ls as $file) {
          // if "file" is a directory
          if ((int) system("file ${path}/${file} | grep 'directory' | wc -l") == 1)
                rek ("${path}/${file}");
          else if ((int) system("file ${path}/${file} | grep 'image' | wc -l") == 1)
               echo "<img src='${path}/${file}' />";
     }
}
 
//you call function using:
rek ("path/to/images/folder");
 
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Showing All Images In Folder

Post by nowaydown1 »

I believe that glob (http://us3.php.net/glob) is also available on PHP 4.
Post Reply