SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Can't make it to sort...

    Good day to you all,
    here i'm working on a function that read a directory, for each folder show its images.

    Here's the part that don't work :
    No error but don't sort while it list files it should also sort them , Portrait, Landscape, Panoramic.

    Here is my code :
    PHP Code:



    <?php

            
             set_time_limit
    (0);
            

            
            
    $directory $_GET['dir'];

    // Function that read a directory list of folder and result them
    function getDirectory$path '.'$level ){

        
    $ignore = array( 'cgi-bin''.''..' );
        
    // Directories to ignore when listing output. Many hosts
        // will deny PHP access to the cgi-bin.

        
    $dh = @opendir$path );
        
    // Open the directory to the handle $dh
        
        
    while( false !== ( $file readdir$dh ) ) ){
        
    // Loop through the directory
        
            
    if( !in_array$file$ignore ) ){
            
    // Check that this file is not to be ignored
                
                
    $spaces str_repeat'&nbsp;', ( $level ) );
                
    // Just to add spacing to the list, to better
                // show the directory tree.
                
                
    if( is_dir"$path/$file) ){
                
    // Its a directory, so we need to keep reading down...
                

    //Creating var for Portrait images list    
        
    $portrait "";
    //Creating var for Landscape images list    
    $landscape "";
    //Creating var for panoramic images list    
    $panoramic "";
    //Read that directory
    $files scandir($path."/".$file);
    // Echo the name
    echo "<div id=\"bar\"><b>".$file."</b></div><br/>";
    echo 
    "<div>";

    //For each file in tha folder
    foreach($files as $key => $value){
        
        if (
    $value != "." && $value != "..") {
            
            
    //Substring to $value his last 5 char
            
    $formatfind =  substr($value,-5);
            
    // Subtracting to this the last 4
            
    $format =  substr($formatfind,0,-4);
            
    //This is in my naming convension.Portrait equal 1    landscape equal 0 and panoramic equal 2
            
        // if file $format equal 1
        
    if ($format "1") {
    // add it to portrait list
        
    $portrait .= "<a onmouseover=this.style.cursor=\"pointer\" ' onfocus='this.blur();' onclick=\"document.getElementById('".$value."').style.display = 'block' \" ><img src=\"".$path."".$file."/".$value."\" width=\"50px\" class=\"imag\"></a>\n"
        
    $portrait .= "<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' \" >";
        
    $portrait .= "<img src=\"".$path."".$file."/".$value."\" /><br />".$value."<br/></div>";
        }
        
    // if  file $format equal 0    
        
    if ($format "0") {
        
    $landscape .= "<a onmouseover=this.style.cursor=\"pointer\" ' onfocus='this.blur();' onclick=\"document.getElementById('".$value."').style.display = 'block' \" ><img src=\"".$file."/".$value."\" width=\"50px\" class=\"imag\"></a>\n"
        
    $landscape .= "<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' \" >";
        
    $landscape .= "<img src=\"".$file."/".$value."\" /><br />".$value."<br/></div>";
        
        }
        
        
    // if  file $format equal 2    
        
    if ($format "2") {
        
    $panoramic .= "<a onmouseover=this.style.cursor=\"pointer\" ' onfocus='this.blur();' onclick=\"document.getElementById('".$value."').style.display = 'block' \" ><img src=\"".$file."/".$value."\" width=\"50px\" class=\"imag\"></a>\n"
        
    $panoramic .= "<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' \" >";
        
    $panoramic .= "<img src=\"".$file."/".$value."\" /><br />".$value."<br/></div>";
        
        }
      
    // Else, do nothing
        
    }else{
            
            
            }
        
    }
    // echo result of the 3 format
    echo $portrait;
    echo 
    "<br/>";
    echo 
    $landscape;
    echo 
    "<br/>";
    echo 
    $panoramic;

    echo 
    "</div>";
    echo 
    "<br/>";
                    
                    
              
                }
            
            }
        
        }
        
    // close diretory
        
    closedir$dh );
        
    // Close the directory handle


            
            
    // calling the function
             
    getDirectory$directory );
            
            
     
    ?>

    Thanks !

  2. #2
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Peuplarchie View Post
    Good day to you all,
    here i'm working on a function that read a directory, for each folder show its images.

    Here's the part that don't work :
    No error but don't sort while it list files it should also sort them , Portrait, Landscape, Panoramic.

    Here is my code :
    PHP Code:



    <?php

            
             set_time_limit
    (0);
            

            
            
    $directory $_GET['dir'];

    // Function that read a directory list of folder and result them
    function getDirectory$path '.'$level ){

        
    $ignore = array( 'cgi-bin''.''..' );
        
    // Directories to ignore when listing output. Many hosts
        // will deny PHP access to the cgi-bin.

        
    $dh = @opendir$path );
        
    // Open the directory to the handle $dh
        
        
    while( false !== ( $file readdir$dh ) ) ){
        
    // Loop through the directory
        
            
    if( !in_array$file$ignore ) ){
            
    // Check that this file is not to be ignored
                
                
    $spaces str_repeat'&nbsp;', ( $level ) );
                
    // Just to add spacing to the list, to better
                // show the directory tree.
                
                
    if( is_dir"$path/$file) ){
                
    // Its a directory, so we need to keep reading down...
                

    //Creating var for Portrait images list    
        
    $portrait "";
    //Creating var for Landscape images list    
    $landscape "";
    //Creating var for panoramic images list    
    $panoramic "";
    //Read that directory
    $files scandir($path."/".$file);
    // Echo the name
    echo "<div id=\"bar\"><b>".$file."</b></div><br/>";
    echo 
    "<div>";

    //For each file in tha folder
    foreach($files as $key => $value){
        
        if (
    $value != "." && $value != "..") {
            
            
    //Substring to $value his last 5 char
            
    $formatfind =  substr($value,-5);
            
    // Subtracting to this the last 4
            
    $format =  substr($formatfind,0,-4);
            
    //This is in my naming convension.Portrait equal 1    landscape equal 0 and panoramic equal 2
            
        // if file $format equal 1
        
    if ($format "1") {
    // add it to portrait list
        
    $portrait .= "<a onmouseover=this.style.cursor=\"pointer\" ' onfocus='this.blur();' onclick=\"document.getElementById('".$value."').style.display = 'block' \" ><img src=\"".$path."".$file."/".$value."\" width=\"50px\" class=\"imag\"></a>\n"
        
    $portrait .= "<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' \" >";
        
    $portrait .= "<img src=\"".$path."".$file."/".$value."\" /><br />".$value."<br/></div>";
        }
        
    // if  file $format equal 0    
        
    if ($format "0") {
        
    $landscape .= "<a onmouseover=this.style.cursor=\"pointer\" ' onfocus='this.blur();' onclick=\"document.getElementById('".$value."').style.display = 'block' \" ><img src=\"".$file."/".$value."\" width=\"50px\" class=\"imag\"></a>\n"
        
    $landscape .= "<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' \" >";
        
    $landscape .= "<img src=\"".$file."/".$value."\" /><br />".$value."<br/></div>";
        
        }
        
        
    // if  file $format equal 2    
        
    if ($format "2") {
        
    $panoramic .= "<a onmouseover=this.style.cursor=\"pointer\" ' onfocus='this.blur();' onclick=\"document.getElementById('".$value."').style.display = 'block' \" ><img src=\"".$file."/".$value."\" width=\"50px\" class=\"imag\"></a>\n"
        
    $panoramic .= "<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' \" >";
        
    $panoramic .= "<img src=\"".$file."/".$value."\" /><br />".$value."<br/></div>";
        
        }
      
    // Else, do nothing
        
    }else{
            
            
            }
        
    }
    // echo result of the 3 format
    echo $portrait;
    echo 
    "<br/>";
    echo 
    $landscape;
    echo 
    "<br/>";
    echo 
    $panoramic;

    echo 
    "</div>";
    echo 
    "<br/>";
                    
                    
              
                }
            
            }
        
        }
        
    // close diretory
        
    closedir$dh );
        
    // Close the directory handle


            
            
    // calling the function
             
    getDirectory$directory );
            
            
     
    ?>

    Thanks !


    Hi,

    it seems when you are comparing you made an error. Maybe a typing, maybe a logic. Look at the 3 lines:

    PHP Code:
    if ($format "1") {

    if (
    $format "0") {

    if (
    $format "2") { 

    This way you are not comparing but you are assigning a value to $format.

    It should be this:
    PHP Code:
    if ($format == "1") {

    if (
    $format == "0") {

    if (
    $format == "2") { 

    also, you might want to look into glob for directory reading:

    http://php.net/manual/en/function.glob.php
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  3. #3
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Addendum - particularly since glob can be used to require an extension. glob('*.jpg') will pull all jpeg files for example.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •