简体   繁体   中英

create a multi-dimentional php array for files hierarchy

i'm new in php and i want to create an hierarchy tree with the parent id as you can see in the example, i want to make like this array result : i want to generate an array like that because i want send the array to jstree that i use already

Array
(
[0] => Array
    (
        [text] => Folder name 1
        [parent_id] => 
        [id] => 1
        [children] => Array
            (
                [0] => Array
                    (
                        [text] => Folder name 2
                        [parent_id] => 1
                        [id] => 2
                        [children] => Array
                            (
                            )

                        [data] => stdClass Object
                            (
                            )

                    )

            )

        [data] => stdClass Object
            (
            )

    )
)

This is my own function result

Array
(
[/] => Array
    (
        [0] => .
        [1] => ..
        [2] => .bash_logout
        [3] => .bash_profile
        [4] => .bashrc
        [5] => .contactemail
        [6] => .cpanel
        [7] => .cphorde
        [8] => .ftpquota
        [9] => .gemrc
        [10] => .htpasswds
        [11] => .lastlogin
        [12] => .trash
        [13] => .zshrc
        [14] => access-logs
        [15] => etc
        [16] => logs
        [17] => mail
        [18] => perl5
        [19] => public_ftp
        [20] => public_html
        [21] => ssl
        [22] => tmp
        [23] => www
    )

[etc] => Array
    (
        [0] => .
        [1] => ..
        [2] => cacheid
        [3] => ftpquota
        [4] => hebergementweb-maroc.com
    )

[logs] => Array
    (
        [0] => .
        [1] => ..
        [2] => ftp.hebergementweb-maroc.com-ftp_log-Dec-2017.gz
        [3] => hebergementweb-maroc.com-Dec-2017.gz
    )

[mail] => Array
    (
        [0] => .
        [1] => ..
        [2] => .Drafts
        [3] => .Junk
        [4] => .Sent
        [5] => .Trash
        [6] => cur
        [7] => hebergementweb-maroc.com
        [8] => mailbox_format.cpanel
        [9] => new
        [10] => tmp
    )

[tmp] => Array
    (
        [0] => .
        [1] => ..
        [2] => analog
        [3] => awstats
        [4] => cpbandwidth
        [5] => webalizer
        [6] => webalizerftp
        [7] => .
        [8] => ..
        [9] => analog
        [10] => awstats
        [11] => cpbandwidth
        [12] => webalizer
        [13] => webalizerftp
    )

[perl5] => Array
    (
        [0] => .
        [1] => ..
    )

[public_ftp] => Array
    (
        [0] => .
        [1] => ..
        [2] => incoming
    )

[public_html] => Array
    (
        [0] => .
        [1] => ..
        [2] => .htaccess
        [3] => 404.shtml
        [4] => cgi-bin
        [5] => default.htm
        [6] => error_log
        [7] => favicon.ico
        [8] => phpinfo.php
        [9] => robots.txt
        [10] => test.php
        [11] => under_construction.html
    )

[ssl] => Array
    (
        [0] => .
        [1] => ..
        [2] => certs
        [3] => csrs
        [4] => keys
        [5] => ssl.db
        [6] => ssl.db.cache
    )

[www] => Array
    (
        [0] => .
        [1] => ..
        [2] => .htaccess
        [3] => 404.shtml
        [4] => cgi-bin
        [5] => default.htm
        [6] => error_log
        [7] => favicon.ico
        [8] => phpinfo.php
        [9] => robots.txt
        [10] => test.php
        [11] => under_construction.html
    )
)

and this is my functiosn code :

function ftpRecursiveFileListing($ftpConnection, $path) {
static $allFiles = array();
$contents = ftp_nlist($ftpConnection, $path);

foreach($contents as $currentFile) {
    // assuming its a folder if there's no dot in the name
    if (strpos($currentFile, '.') === false) {
        $this->ftpRecursiveFileListing($ftpConnection, $currentFile);
    }
    $allFiles[$path][] = $currentFile;
            //break;
}
return $allFiles;
}

public function test(){
    $ftp_server = "ftp.mywebsite.com";
    $ftp_user_name = "user@mywebsite.com";
    $ftp_user_pass = "mypswd";
    // Mise en place d'une connexion basique
    $conn_id = ftp_connect($ftp_server);
    // Identification avec un nom d'utilisateur et un mot de passe
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    echo "<pre>";
    $handler = $this->ftpRecursiveFileListing($conn_id, '/');
    print_r($handler);

}

many thanks

You can use local array in order the build the tree.

function ftpRecursiveFileListing($ftpConnection, $path, $parentId) {
    static $cnt = 0;
    $files = array();
    $contents = ftp_nlist($ftpConnection, $path);

    foreach($contents as $currentFile) {
        $currentFileArray = array();
        $currentFileArray[text] = $currentFile;
        $currentFileArray[parentId] = $parentId;
        $currentFileArray[id] = $cnt++;
        // assuming its a folder if there's no dot in the name
        if (strpos($currentFile, '.') === false)
            $currentFileArray[children] = $this->ftpRecursiveFileListing($ftpConnection, $currentFile, $currentFileArray[id]);
        $files [] = $currentFileArray;
    }
return $files ;
}

Call the function for the first time will be with:

$handler = $this->ftpRecursiveFileListing($conn_id, '/', null);

This only basic data - you can add more keys to the arrays

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM