View Single Post
Old 06-09-2009, 06:03 PM   #4 (permalink)
Runar
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

This might just do the trick.

I have tested the code myself and it seems to be working the way you requested. The script will list files and sub-folders in the folder you define; either directly in the script or by $_GET data. It should return data like this:

Code:
* img/ipsum
      o landscape_0.png
            + type: landscape
      o panoramic_2.png
            + type: panorama
      o portrait_1.png
            + type: portrait

* img/landscape_0.png

* img/lorem
      o ipsum
            + no type found!
      o landscape_0.png
            + type: landscape
      o panoramic_2.png
            + type: panorama
      o portrait_1.png
            + type: portrait
You should note that it will probably fail on image names that contains numbers and all kinds of symbols except a-z. To make it possible to use numbers in your image names, I recommend using this format:

1_panorama34.jpg - <image type>_<image name>

Here we go:

PHP Code:
<?php

/**
 * Set the directory to be scanned
 * 
 * If $_GET['dir'] is present, use that
 * Otherwise use 'img'
 **/
$directory = isset( $_GET['dir'] ) ? $_GET['dir'] : 'img';

// Array containing 'bad' files, such as system and hidden files
$bad_files = array( '.''..''.DS_Store''_notes''Thumbs.db' );

echo 
'<h1>Scan folder for sub-folders containing images</h1>
<p>Scanned folder: <strong>' 
$directory '</strong></p>
<h2>Result:</h2>'
;

// Make sure the filename we are checking is a directory
if( is_dir$directory ) )
{    
    
// Scan the directory for sub-directories and files
    
$content scandir$directory );
    
    
// Compare content array against bad files array to remove bad files
    
$files array_diff$content$bad_files );
        
    
// Process files in the directory
    
foreach( $files as $key => $value )
    {
        echo 
'<ul><li>' $directory '/' $value '</li>';
        
        
// Check if the file is a sub-directory
        
if( is_dir'img/' $value ) )
        {            
            echo 
'<ul>'// Sub-directory content lists
            
            // Scan the sub-directory
            
$new_content scandir'img/' $value );
            
            
// Compare new content array against bad files array to remove bad files
            
$new_files array_diff$new_content$bad_files );
            
            
// Process files in the sub-directory
            
foreach( $new_files as $key => $value )
            {
                echo 
'<li>' $value '</li><ul>'// File name and beginning of image type list
                
                /**
                 * Get image type according to number in file name (<filename>_<number>)
                 * 
                 * $matches[0] returns the whole file name including number
                 * $matches[1] returns only the name (before _)
                 * $matches[2] returns only the number (after _)
                 * 
                 * Returns 3 if no number is found
                 **/
                
$image_type preg_match'/([a-z]+)_(\d+)/i'$value$matches ) ? $matches[2] : 3;
                                
                
/**
                 * Display image type based on $image_type
                 * 
                 * If image_type is 3 (or anything except 0-2), error message is displayed
                 * Remove default to ignore the error message
                 **/
                
switch( $image_type )
                {
                    case 
0:
                    echo 
'<li>type: <strong>landscape</strong></li>';
                    break;
                    
                    case 
1:
                    echo 
'<li>type: <strong>portrait</strong></li>';
                    break;
                    
                    case 
2:
                    echo 
'<li>type: <strong>panorama</strong></li>';
                    break;
                    
                    default:
                    echo 
'<li><strong>no type found!</strong></li>';
                    break;
                }
                
                echo 
'</ul>'// Image type list
            
}
            
            echo 
'</ul>'// Sub-directory content lists
        
}
        
        echo 
'</ul>'// Main directory lists
    
}
}

?>
If you experience any kind of problems, please let me know.


Yours, Runar
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
The Following User Says Thank You to Runar For This Useful Post:
Peuplarchie (06-09-2009)