|  Topic awaiting preservation: loading images from a folder (Page 1 of 1)  | |
|---|---|
| Paranoid (IV) Inmate From: London |  posted 07-31-2008 23:41 Hi there, | 
| Paranoid (IV) Inmate From: cell 3736 |  posted 08-01-2008 01:18 Should be very easy in any language but I used PHP... 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;
      }
 code: $folder = "gallery/";
$handle = opendir($folder);  
while(($file = readdir($handle)) !== false)  {    
  if($file != "." && $file != "..")    {      
    echo ($file."</br>");
  }
}
 code: <?
$folder = "gallery/";
$handle = opendir($folder);  
while(($file = readdir($handle)) !== false)  {    
  if($file != "." && $file != "..")    {      
    echo ("<img src=\"".$folder.$file."\"></br>");
  }
}
?>
 | 
| Paranoid (IV) Inmate From: London |  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;
    }
?>
 code: <?php
	echo "<ul>\n";
	foreach ($list as $image) {
	    echo "\t<li><img src=\"images/{$image}\" /></li>\n";
	}
	echo "</ul>";
?>
 | 
| Paranoid (IV) Inmate From: cell 3736 |  posted 08-01-2008 12:25 Looks like the first code block does exactly what the "function directory($dir,$filters)" snippet did. 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>
 code: <ul>
<?php
	foreach ($list as $image) {
	    echo "\t<li><img src=\"images/{$image}\" /></li>\n";
	}
?>
</ul>
 | 
| Paranoid (IV) Inmate From: A graveyard of dreams |  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);
 | 
| Paranoid (IV) Inmate From: London |  posted 08-01-2008 14:33 This is all good stuff, cheers. | 
| Paranoid (IV) Inmate From: cell 3736 |  posted 08-01-2008 14:33 
 | 
| Maniac (V) Mad Librarian From: Seoul, Korea |  posted 08-02-2008 06:43 Holy crap... it's Veneficuz! Long time no see!  |