Closed Thread Icon

Topic awaiting preservation: loading images from a folder (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=30453" title="Pages that link to Topic awaiting preservation: loading images from a folder (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: loading images from a folder <span class="small">(Page 1 of 1)</span>\

 
Blaise
Paranoid (IV) Inmate

From: London
Insane since: Jun 2003

posted posted 07-31-2008 23:41

Hi there,

I'm after a little advice for an approach to create a really simple gallery page with few whistles and bells.

I intend to have a page on my site generate a list of images taken from a folder on the webserver, these images can be updated and changed (via ftp).

What would you recommend to use in the way of script? I'd love to use Ruby on Rails, but it seems over kill and php makes me shudder, but I think it may be the most straight forward way to get this done, ASP isn't an option neither is .NET.

Examples I've found on the web often include databases (overkill for me) and or some method of uploading (not necessary), I just want a list generated from images in a folder how easy is this?

Cheers,

Arthurio
Paranoid (IV) Inmate

From: cell 3736
Insane since: Jul 2003

posted posted 08-01-2008 01:18

Should be very easy in any language but I used PHP...
I have a very simple piece of code that I made for my website (www.kaarellumi.com)
I upload a bunch of pictures and then the script generates thumbnails and displays every folder as a sub-gallery. Very easy and comfortable for me ... no fancy backends or whatever.

Found this snippet from somewhere:

code:
/*        Function directory($directory,$filters)
                reads the content of $directory, takes the files that apply to $filter
             		and returns an array of the filenames.
                You can specify which files to read, for example
                $files = directory(".","jpg,gif");
                gets all jpg and gif files in this directory.
                $files = directory(".","all");
                gets all files.*/
      function directory($dir,$filters){	
        $handle=opendir($dir);	
        $files=array();	
        if ($filters == "all"){
          while(($file = readdir($handle))!==false){
            $files[] = $file;
          }
        }	
        if ($filters != "all")	{		
          $filters=explode(",",$filters);		
          while (($file = readdir($handle))!==false){			
            for ($f=0;$f<sizeof($filters);$f++):
              $system=explode(".",$file);				
              if ($system[1] == $filters[$f]){
                $files[] = $file;
              }
            endfor;
          }
        }
        closedir($handle);
        return $files;
      }



Something simpler would look like this:

code:
$folder = "gallery/";
$handle = opendir($folder);  
while(($file = readdir($handle)) !== false)  {    
  if($file != "." && $file != "..")    {      
    echo ($file."</br>");
  }
}



edit: if you edit the echo line then this little snippet should do all that you described

edit: if you are completely unfamiliar with php then do this:
1) make a file called index.php
2) put this in the file:

code:
<?
$folder = "gallery/";
$handle = opendir($folder);  
while(($file = readdir($handle)) !== false)  {    
  if($file != "." && $file != "..")    {      
    echo ("<img src=\"".$folder.$file."\"></br>");
  }
}
?>


3) voila! you see the images from folder called "gallery" or an error message if you don't have that gallery ...

(Edited by Arthurio on 08-01-2008 01:35)

Blaise
Paranoid (IV) Inmate

From: London
Insane since: Jun 2003

posted posted 08-01-2008 10:36

Wow it can be really simple, this is what I came up with last night, the only thing I need to do on it now is make sure the file names all have the prefix 'thumb_', I don't want the thumbnails generated dynamically as they may not look right.

code:
<?php
    $folder = "images/"; // The folder containing the images.    
    $list  = array(); // This will hold data for the images found.
    $valid = array("jpg", "jpeg"); // Images that Flash can load.

    // Open the folder and read the files.
    $dir = opendir($folder);
    while (($file = readdir($dir)) !== false) {
        // Make sure the file is an actual file.
        if (is_file($folder . $file)) {
            // Check for valid file extensions.
            if (hasValidExtension($file)) {
                // Get the image info.
             //   $info->name     = $file;
                // Add the info to the list array.
                $list[] = $file;
            }
        }
    }
    // Close the directory.
    closedir($dir);

    // Check to make sure a file has a valid extension.
    function hasValidExtension($file) {
        global $valid;
        $dot = strrpos($file, ".");
        if ($dot === false) {
            return false;
        }
        $ext = substr($file, $dot+1, 4);
        foreach ($valid as $value) {
            if ($ext == $value) {
                return true;
            }
        }
        return false;
    }
?>


Followed by...

code:
<?php
	echo "<ul>\n";
	foreach ($list as $image) {
	    echo "\t<li><img src=\"images/{$image}\" /></li>\n";
	}
	echo "</ul>";
?>



Is this too bad? Although I'm hating the idea that I have to mess up my HTML so much

Arthurio
Paranoid (IV) Inmate

From: cell 3736
Insane since: Jul 2003

posted posted 08-01-2008 12:25

Looks like the first code block does exactly what the "function directory($dir,$filters)" snippet did.

I would do something like this:

1) put the directory function into library.php

2)

code:
<?php
    import("library.php");
    $folder = "images/"; // The folder containing the images.
    $valid = "jpg,jpeg"; // Images that Flash can load.
    $list  = directory($folder, $valid); // This will hold data for the images found.
    
    echo ("<ul>\n");

    foreach ($list as $image) {
	echo "\t<li><img src=\"images/{$image}\" /></li>\n";
    }

?>
</ul>





About the second code block: you don't need to do echo "<ul>\n"; and echo "</ul>"; if they are next to <? or ?> ...

code:
<ul>
<?php
	foreach ($list as $image) {
	    echo "\t<li><img src=\"images/{$image}\" /></li>\n";
	}
?>
</ul>



is the same

Veneficuz
Paranoid (IV) Inmate

From: A graveyard of dreams
Insane since: Mar 2001

posted posted 08-01-2008 14:14

The glob function is another option to use if you don't want to write your own directory listing function.

code:
$list = glob($folder."thumb_*.jp{,e}g",  GLOB_BRACE);



You have to hard-code the search string, so it might not be as elegant as the other solutions. But if that isn't an issue this solution works fine, and I find it to be a more elegant solution due to its' simplicity. It is possible to encapsulate the glob function in another function that takes the search parameters and create 'glob strings', but that would remove most of the simple elegance of using glob.

Edit: And Hi everyone, both new and old inmates!
_________________________
- the Golden Ratio

(Edited by Veneficuz on 08-01-2008 14:21)

Blaise
Paranoid (IV) Inmate

From: London
Insane since: Jun 2003

posted posted 08-01-2008 14:33

This is all good stuff, cheers.

Arthurio
Paranoid (IV) Inmate

From: cell 3736
Insane since: Jul 2003

posted posted 08-01-2008 14:33

... didn't know about that one ... very interesting

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 08-02-2008 06:43

Holy crap... it's Veneficuz! Long time no see!


___________________________
Suho: www.liminality.org | Cell 270 | Sig Rotator | the Fellowship of Sup

« BackwardsOnwards »

Show Forum Drop Down Menu