Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts

    sorting multi-dimensional array in PHP class

    I've quickly written a PHP class to get info about each file in a directory and its sub-directories....

    I've got it practically working as it should, except for one small problem that I've noticed...When you sort by "file_display_name" (See the example usage), the results aren;t ordered correctly....Rather, most of the results/files are ordered ok EXCEPT for the last few results displayed....there seems to be a few out-of-order results/files displayed randomly at the end of the file list ....And I can;t seem to figure out why...it has something to do with the MultiArraySort() method, I think....

    Edit: Here is an example of the problem: Click Me! ...I'm searching a directory called '/var/www/html/pear/' using the code from GetDirFiles_example.php in the next post...

    Any help is appreciated (I appreciate anyone who is willing to spend a little time to sort through this code)...Here is the updated class file (the example usages appear in my next post since this will probably be too long for one post):

    PHP Code:
    <? 
     
    // file GetDirFiles.class.php 
     
     
    /*********************** 
     
    Class returns a multi-dimensional array containing different aspects of each file in a directory and its sub-directories. See sample usage: GetDirFiles_example.php and GetDirFiles_example2.php. 
     
    ************************/ 
     
     
    class GetDirFiles 

        var 
    $directory
        var 
    $base_dir
        var 
    $file_array
        var 
    $accepted_file_types
        var 
    $count
        var 
    $disable_title_tags
        var 
    $sort_by
        var 
    $exceptions
        var 
    $excluded_dirs
        var 
    $excluded_files
     
    function 
    GetDirFiles($directory,$accepted_file_types,$excluded_dirs=0,$excluded_files=0,$exceptions=0,$disabl e_title_tags="false",$sort_by="title_tags"
        { 
            
    $this->directory $directory
            
    $this->accepted_file_types $accepted_file_types
            
    $this->disable_title_tags $disable_title_tags
            
    $this->sort_by $sort_by
            
    $this->exceptions $exceptions
            
    $this->excluded_dirs $excluded_dirs
            
    $this->excluded_files $excluded_files
     
            
    $this->base_dir $this->directory
        } 
     
        function 
    CreateFileArray() 
        { 
            
    $this->get_files(); 
            
    $this->find_subdirs(); 
            
    $this->file_array $this->MultiArraySort($this->sort_bySORT_ASCSORT_STRING$this->file_array); 
            return 
    $this->file_array
        } 
     
        function 
    get_files() 
        { 
            
    $dir_pattern preg_replace('/\//','\/',$this->base_dir); 
     
            
    $handle opendir($this->directory); 
     
            while (
    false !== ($file readdir($handle))) 
            { 
                
    // read all files in dir 
     
                  
    if ($file != "." && $file != ".."
                  { 
                    
    $ext stristr($file'.'); 
     
                    if (
    in_array($ext,$this->accepted_file_types)) 
                    { 
                        if (
    $this->excluded_files != 0
                        { 
                            if (
    in_array($file,$this->excluded_files)) 
                            { 
                                continue; 
                            } 
                        } 
     
                        if (
    $this->count==1
                        { 
                            
    $pattern1 '/(' $dir_pattern ')(.*?)/'
                            
    $dir preg_replace($pattern1,'$2',$this->directory); 
     
                            if (!empty(
    $dir)) 
                            { 
                                
    $the_path $dir.$file
                            } 
                            else 
                            { 
                                
    $the_path $file
                            } 
                        } 
                        else 
                        { 
                            
    $the_path $file
                        } 
     
                        
    $path_array explode("/",$the_path); 
                        
    $file_display_name ucfirst($path_array[count($path_array)-1]); 
     
                        if (
    $this->disable_title_tags == "false"
                        { 
                            
    $title_tags $this->get_title_tags($file); 
    $this->file_array[] = array('file_display_name' => $file_display_name'the_path' => $the_path'title_tags' => $title_tags); 
                        } 
                        else 
                        { 
    $this->file_array[] = array('file_display_name' => $file_display_name'the_path' => $the_path); 
                        } 
                    } 
                } 
            } 
     
            
    closedir($handle); 
            
    $this->count=1
        } 
     
        function 
    find_subdirs() 
        { 
           
    // load subdirectories into array 
     
           
    $handle opendir($this->directory); 
     
           while (
    $file readdir($handle)) 
           { 
                
    // read all files in dir 
     
                  
    if ($file != ".." && $file != "."
                  { 
                    
    $abs_file $this->directory $file
     
                    if (
    is_dir($abs_file)) 
                    { 
                        if (
    $this->excluded_dirs != 0
                        { 
                            if (
    in_array($file,$this->excluded_dirs)) 
                            { 
                                continue; 
                            } 
                        } 
     
                        
    $subdirs[count($subdirs)] = $abs_file
                     } 
                  } 
           } 
     
           
    closedir($handle); 
     
           if (
    is_array($subdirs)) 
           { 
              
    sort($subdirs); 
     
              foreach (
    $subdirs as $key => $val
              { 
                 
    $val str_replace("//","/",$val) . "/"
                 
    $this->directory $val
                 
    $this->get_files($val); 
                 
    $this->find_subdirs($val); 
              } 
           } 
        } 
     
        function 
    get_title_tags($file
        { 
            
    // Open file and extract title tag contents 
     
            
    $abs_file $this->directory $file
     
            
    $handle = @fopen($abs_file"r"); 
            if (
    $handle
            { 
                  while (!
    feof($handle)) 
                  { 
                       
    $file_content fgets($handle4096); 
                       if (
    preg_match('/(<title>)(.*?)(<\/title>)/i'$file_content$matches) == 1
                       { 
                        
    $page_title $matches[2]; 
                        break; 
                       } 
                   } 
                   
    fclose($handle); 
            } 
     
            if (!empty(
    $page_title)) 
            { 
                if (
    $this->exceptions != 0
                { 
                    foreach (
    $this->exceptions as $key => $val
                    { 
                        if (
    $this->exceptions[$key]['append_to_end'] == "no"
                        { 
                            
    $exceptions_no[$key] = $val
                        } 
                        else 
                        { 
                            
    $exceptions_yes[$key] = $val
                        } 
                    } 
     
                    if (!empty(
    $exceptions_no)) 
                    { 
                        foreach (
    $exceptions_no as $key => $val
                        { 
                            if (
    strpos($page_title$exceptions_no[$key]['text']) === 0
                            { 
                                
    $page_title trim(substr($page_title$exceptions_no[$key]['char_position'])); 
                            } 
                        } 
                    } 
     
                    if (!empty(
    $exceptions_yes)) 
                    { 
                        foreach (
    $exceptions_yes as $key => $val
                        { 
                            if (
    strpos($page_title$exceptions_yes[$key]['text']) === 0
                            { 
                                
    $page_title trim(substr($page_title$exceptions_yes[$key]['char_position'])); 
                                
    $page_title .= ', ' ucfirst($exceptions_yes[$key]['text']); 
                            } 
                        } 
                    } 
                } 
     
                return 
    $page_title
            } 
            else 
            { 
                return 
    false
            } 
        } 
     
        function 
    MultiArraySort() 
        { 
            
    $n func_num_args(); 
            
    $ar func_get_arg($n-1); 
            if (!
    is_array($ar)) 
            { 
                return 
    false
            } 
     
            for (
    $i 0$i $n-1$i++) 
            { 
                
    $col[$i] = func_get_arg($i); 
            } 
     
            foreach (
    $ar as $key => $val
            { 
                foreach (
    $col as $kkey => $vval
                { 
                    if (
    is_string($vval)) 
                    { 
                        ${
    "subar$kkey"}[$key] = $val[$vval]; 
                    } 
                } 
            } 
     
            
    $arv = array(); 
            foreach (
    $col as $key => $val
            { 
                
    $arv[] = (is_string($val) ? ${"subar$key"} : $val); 
            } 
            
    $arv[] = $ar
     
            @
    call_user_func_array("array_multisort"$arv); 
            return 
    $ar
        } 

     
    ?>
    Last edited by chump2877; 08-24-2006 at 04:01 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    The example usage files (there are 2 examples):

    PHP Code:
    <? 
     
    // file GetDirFiles_example.php 
     
    // Configure settings 
    $directory '/absolute/path/to/directory/';  // Absolute path of directory to search 
    $accepted_file_types = array(".html",".shtml",".php",".jpg");  // Enter file types to search for 
    $disable_title_tags "true";  // Record title tag contents of each file in array. Default is 'false'. 
    $sort_by "file_display_name";  // Sort by either 'file_display_name' (name of file), 'the_path' (path of file, minus value of $directory), or 'title_tags' (content of file title tags). Default is 'title_tags'. 
     
    include 'GetDirFiles.class.php'
    $handle = new GetDirFiles($directory,$accepted_file_types,$disable_title_tags,$sort_by); 
    $file_array $handle->CreateFileArray(); 
     
    $num=1
    foreach (
    $file_array as $key => $val

        echo 
    "<p style='font:bold 16px Times New Roman;text-decoration:underline'>File #$num</p>"
        echo 
    "<p style='margin-top:5px'>Name: ".$file_array[$key]['file_display_name']."<br>"
        echo 
    "Truncated File Path: ".$file_array[$key]['the_path']."</p>"
        
    $num++; 

     
    ?>
    PHP Code:
    <? 
     
    // file GetDirFiles_example2.php 
     
    // Configure settings 
    $directory '/absolute/path/to/directory/';  // Absolute path of directory to search 
    $accepted_file_types = array(".html",".shtml",".php");  // Enter file types to search for 
    $excluded_dirs = array("directory1","directory2","directory3");   // Array containing a list of directories that you want excluded from the search. Default is set to zero (no excluded directories). 
    $excluded_files = array("file1.html","file2.php","file3.shtml");   // Array containing a list of files that you want excluded from the search. Default is set to zero (no excluded files). 
    $disable_title_tags "false";  // Record title tag contents of each file in array. Default is 'false'. 
    $sort_by "title_tags";  // Sort by either 'file_display_name' (name of file), 'the_path' (path of file, minus value of $directory), or 'title_tags' (content of file title tags). Default is 'title_tags'. 
    $exceptions = array( 
            array(
    "text" => "Text1""char_position" => 15"append_to_end" => "no"), 
            array(
    "text" => "Text2""char_position" => 12"append_to_end" => "no"), 
            array(
    "text" => "Text3""char_position" => 4"append_to_end" => "yes"
    );  
    // A multi-dimensional array containing exceptions to the way the title tag should be returned. "text" is the text that will be truncated from the start of the title tag contents, "char_position" is the numerical position of the first character of the title tag text after the exception text, and "append_to_end" gives you the option to append the exception text to the end of the title tag contents. Default is set to zero (no exceptions). 
     
    include 'GetDirFiles.class.php'
    $handle = new GetDirFiles($directory,$accepted_file_types,$excluded_dirs,$excluded_files,$exceptions,$disable_titl  e_tags,$sort_by); 
    $file_array $handle->CreateFileArray(); 
     
    $num=1
    foreach (
    $file_array as $key => $val

        if (!empty(
    $file_array[$key]['title_tags'])) 
        { 
            echo 
    "<p style='font:bold 16px Times New Roman;text-decoration:underline'>File #$num</p>"
            echo 
    "<p style='margin-top:5px'>Name: ".$file_array[$key]['file_display_name']."<br>"
            echo 
    "Truncated File Path: ".$file_array[$key]['the_path']."<br>"
            echo 
    "Title Tag Content: ".$file_array[$key]['title_tags']."</p>"
            
    $num++; 
        } 

     
    ?>

    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

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