TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-09-2009, 04:33 PM   #1 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Application Scan dir, seperate file by name convension...

Good day to you all,
here I have a script that show all images in a directory.

My questions :
1. How can I make my scan dir for each folder listed in the specified folder, only 1 level.
2. My naming convension is adding a "_1" at the end of the portrait images, "_0" to the landscape and "_2" to the end of a panoramic image.

How can I seperate the result ?

So basicly :

ACDC
Portrait images
Landscape images
Panoramic images

Aerosmith
Portrait images
Landscape images
Panoramic images

Here is my code right now :

PHP Code:



<?php

         set_time_limit
(0);
        
        
        
$dir $_GET['dir'];
$directory $dir;

$diri $directory//You could add a $_GET to change the directory
$files scandir($diri);
echo 
"<div id=\"bar\"><b>".$directory."</b></div><br/>";
echo 
"<div>";
foreach(
$files as $key => $value){
    
    if (
$value != "." && $value != "..") {

    echo 
"<a onmouseover=this.style.cursor=\"pointer\" ' onfocus='this.blur();' onclick=\"document.getElementById('".$value."').style.display = 'block' \" ><img src=\"".$directory."/".$value."\" width=\"50px\" class=\"imag\"></a>\n"
    echo 
"<div id='".$value."' style='display: none;  position: absolute;  text-align:left; margin: 0px -300px; margin-top:-150px;  z-index:50; border: solid black 1px; padding: 10px; background-color: #ffffff; text-align: justify; font-size: 12px; onmouseover='this.style.cursor=\"pointer\" ' style='font-size: 12px;' onfocus='this.blur();' onclick=\"document.getElementById('".$value."').style.display = 'none' \" >";
    echo 
"<img src=\"".$directory."/".$value."\" /><br />".$value."<br/></div>";
    
    
    }else{
        
        echo 
"";
        
        }
    
}
echo 
"</div>"

 
?>
Thanks !
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 06-09-2009, 04:39 PM   #2 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

Just a quick question before I start working on it. What is the point of this?
PHP Code:
$dir $_GET['dir'];
$directory $dir;

$diri $directory
Will not $diri contain the same as $_GET['dir']?
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
Old 06-09-2009, 04:44 PM   #3 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Default

PHP Code:

$dir 
$_GET['dir'];
// receive directory by url "ACDC/Photos/"
$directory $dir;
// I took of the part that edit the directory for later use
$diri $directory;  
// I took of the part that edit the directory for later use 
__________________
That's why we are not alone on earth... let's build !

Last edited by Peuplarchie : 06-09-2009 at 05:22 PM.
Peuplarchie is offline  
Reply With Quote
Old 06-09-2009, 06:03 PM   #4 (permalink)
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)
Old 06-09-2009, 07:25 PM   #5 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Default

Wow, it had to be the other way around !
Thanks, nice !
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 06-09-2009, 07:38 PM   #6 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

Glad I could help! Let me know if it works the way you want :)


Yours, Runar
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy to Modify Login Script with Hierarchical User Permissions and XML Account File Wildhoney Script Giveaway 4 05-04-2011 06:11 AM
Where is my file? superthin General 3 07-25-2009 09:48 AM
Aptana Jaxer and file uploads xenon Advanced PHP Programming 2 06-06-2008 10:22 AM
Writing to XML file buildakicker General 8 02-06-2008 08:17 PM
Using class methods in an included file? Andrew Advanced PHP Programming 6 12-23-2007 03:20 AM


All times are GMT. The time now is 07:34 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design