Welcome Guest, Not a member yet? Register   Sign In
directory_map - directory helper -- scandir vs readdir
#1

[eluser]nikefido[/eluser]
Hi everyone -

I am using the directory helper for a file management project - it reads a directory and eventually shows a list of files and sub files / directories.

Anyway, the list returned seems to be in no particular order.


I was hoping that I could use "scandir" to replace "readdir" in the function successfully.
This would (potentially) allow me to view files in alphabetical order.

Any thoughts on this?
#2

[eluser]nikefido[/eluser]
maybe I can just order the array returned from the stock helper function with an array sorting function...

EDIT: naturally, "sort" is not recursive AND resets associative array keys to numbers.

so that leaves functions where I have to write my own sorting function and to keep the associative array keys.
#3

[eluser]TheFuzzy0ne[/eluser]
[quote author="nikefido" date="1209172805"]maybe I can just order the array returned from the stock helper function with an array sorting function...[/quote]

That would make sense, but you might find that the files get listed before the directory names. I think you may need to order them, and then start reading the numbered entries, and then the ones that start with words.
#4

[eluser]nikefido[/eluser]
Code:
function sort_array($array) {
    
        function doIt($array) {
            foreach($array as $k => $v) {
                if(is_array($v)) {
                    uasort($v, 'isort');
                    doIt($v);
                }
            }
        }//end nested function
        function isort($a,$b){
                if(ord(substr(strtolower($a),0,1)) == ord(substr(strtolower($b),0,1))) { return 0;}
                return (ord(substr(strtolower($a),0,1)) < ord(substr(strtolower($b),0,1))) ? -1 : 1;
        } //end nested function
        doIt($array);
    }//end function

besides this being horibbly coded with nested functions, can anyone tell me why I am getting a warning / error on this?


================================================
PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: controllers/userPanel.php

Line Number: 109
================================================

Line 109 is
Code:
if(ord(substr(strtolower($a),0,1)) == ord(substr(strtolower($b),0,1))) { return 0;}
#5

[eluser]Elliot Haughin[/eluser]
Hey... so, here's some initial thoughs.

scandir() is a PHP5 function, so isn't compatible backwards. Be aware of this from the start.

Code:
if(ord(substr(strtolower($a),0,1)) == ord(substr(strtolower($b),0,1))) { return 0;}

This is most likely because one of the values you're doing a string function on is not a string... it's an array.

You need to ensure the 2 values sent to isort() are strings, not arrays. So further have a look closely to make sure your recursive pattern works as expected with the array you send it. Echo out the output at various points so you can logically test the function is doing what it's supposed to.

It looks like a logical error rather than a syntax or functional error.
#6

[eluser]nikefido[/eluser]
OK thanks - its making sense.

I need to test within the isort function if $a / $b is a function (and skip it if so). I'm sure that brings up another problem (like not comparing the one of the values $a or $b if one is an array)...I hope that makes some sense.

Maybe I can do this from another POV - perhaps using system() and a linux command to organize folders in a file a certain way?


argh, this is a hard problem Sad
#7

[eluser]nikefido[/eluser]
hey all - just wanted to let you all in on my solution, and my last issue to conquer with it.

So I read my venerable old "PHP5 Advanced" book (Visual quickpro guide) about array sorting and came up with my hacked version of directory_helper. Keep in mind I use this function to see directory structure for users of a file management project I am developing.

Here is my version:
Code:
if (! function_exists('directory_map'))
{
    function directory_map($source_dir, $top_level_only = FALSE)
    {    
        if ($fp = @opendir($source_dir))
        {
            $filedata = array();
            while (FALSE !== ($file = readdir($fp)))
            {
                if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.' AND $top_level_only == FALSE)
                {
                    $temp_array = array();
                
                    $temp_array = directory_map($source_dir.$file."/");
                
                    $filedata[$source_dir.$file] = $temp_array; //$k => directory path; $v => directory files && sub-directories
                    uksort($filedata[$source_dir.$file], 'mysort');//this will always be an array, so sort it here!
                }
                elseif (substr($file, 0, 1) != ".")
                {
                    $filedata[$source_dir.$file] = $source_dir.$file; //$k => file path; $v => file path
                }
            }
            return $filedata;
        }
    }
}

function mysort($x, $y) {
    return strcasecmp($x, $y);
}

Notice that I use the uksort function within the helper in order to get around doing another recursive function to sort the arrays after the directory_helper function.

My final hump was that I needed to do the uksort function one last time in order to order the very top tier. This worked when I just do (in testing within the directory_helper.php file)
Code:
$myarray = directory_map('path/to/folder/');
uksort($myarray, 'mysort');
print_r($myarray);

However, in "production", I had to do this("this" will seem obvious but I had to go through a hoop or two on my own to get to it Big Grin ):

Code:
$this->load->helper('directory');
$directory = directory_map('path/to/folder/');
uksort($directory, 'mysort'); //final sort to top tier of items - didn't have to re=declare a sorting function since it is included with my hacked directory_helper




Theme © iAndrew 2016 - Forum software by © MyBB