Search code examples
phpfiledirectorydirectory-structurefancytree

Fancytree - Generate tree with php


I am using Fancytree, I want to generate a file/folder tree but excluding all folders that are not from the current user.

Example:

UPPERCASE = FOLDER
lowercase = file

I want to create a tree structure with the user "Peter"

Tree image

I have several problems:

  1. I don't understand the listFolders function, I can't give it a parameter (like $ name_user) otherwise it doesn't work.
  2. the lines >> print_r ('TAKE THIS'. $ Dir); << show what I want to take, but it is foreach which does not work
  3. my code is duplicated because I can't create a function inside a function ...

Here is my code:

    <?php
    $name_user = 'Peter'; // actualy not user :-( 
    print_r(listFolders());
    
    function listFolders($dir = __DIR__ . '/datausers') {
        $dh = scandir($dir);
        $name_user = "Peter";
        $return = [];
    
        if (strpos($dir, "/datausers/PERSONNAL") !== false){ // PERSONNAL found !
            if ($dir === __DIR__ . '/datausers/PERSONNAL') {  
                print_r('TAKE THIS' . $dir);
                echo '</br>';
    
                foreach ($dh as $item) {
                    if ($item != '.' && $item != '..') {
                        if (is_dir($dir . '/' . $item)) { // it's a folder
                            $return[] = array(
                                'title' => $item,
                                'folder' => true,
                                'expanded'=> true,
                                'children' => listFolders($dir . '/' . $item, $key)
                            );
                        } else { // it's a file
                            $return[] = [
                                'title' => $item,
                            ];
                        }
                    }
                }
            }
    
            if (strpos($dir, "/datausers/PERSONNAL/" . $name_user) !== false){ // Peter found
                print_r('TAKE THIS' . $dir);
                echo '</br>';
    
                foreach ($dh as $item) {
                    if ($item != '.' && $item != '..') {
                        if (is_dir($dir . '/' . $item)) { // it's a folder
                            $return[] = array(
                                'title' => $item,
                                'folder' => true,
                                'expanded'=> true,
                                'children' => listFolders($dir . '/' . $item, $key)
                            );
                        } else { // it's a file
                            $return[] = [
                                'title' => $item,
                            ];
                        }
                    }
                }
            }
        } 
        else {
            print_r('TAKE THIS' . $dir);
            echo '</br>';
    
            foreach ($dh as $item) {
                if ($item != '.' && $item != '..') {
                    if (is_dir($dir . '/' . $item)) { // it's a folder
                        $return[] = array(
                            'title' => $item,
                            'folder' => true,
                            'expanded'=> true,
                            'children' => listFolders($dir . '/' . $item, $key)
                        );
                    } else { // it's a file
                        $return[] = [`enter code here`
                            'title' => $item,
                        ];
                    }
                }
            }
        }
        return $return;
    }
    ?>

Solution

  • Ok I found the solution. It's crazy I spent the day yesterday on this problem. and today I finally find the solution. Thank you requinix for answering me.

    I put here my solution (not perfect) but it works.

    <?php
    if(session_status() != 2) { session_start(); } else { header('Location: deco.php'); } // (2 = session active) if don't active session
    if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {header('Location: deco.php');} // redirection si pas de session ouverte
    if (time() > $_SESSION['expire']) {
        session_destroy();
        header('Location: deco.php');
    }
    $e1 = isset($_POST['e1']) ? $_POST['e1'] : null;
    $e2 = isset($_POST['e2']) ? $_POST['e2'] : null;
    if (empty($e1) || empty($e2)) {
        $return_data['key1'] = 'error';
        echo json_encode($return_data);
        return;
    }
    
    if ($e1 == 'read'){
        $e2 = str_replace('<','',$e2); // just for prevent html injection
        $e2 = str_replace('?','',$e2); // just for prevent php injection
        $return_data['key1'] = 'read';
        $return_data['key2'] = $e2;
        echo json_encode($return_data);
        return;
    
        $e2 = str_replace('}','',$e2); 
        $e2 = str_replace('"','',$e2); 
        $e2 = explode(":", $e2);        // take only name node 
    }
    
    if ($e1 == 'tree'){ 
        if ($_SESSION['username'] !== $e2){ // verification right name user
            $return_data['key1'] = 'error';
            $return_data['key2'] = ' tree authentication problem';
            echo json_encode($return_data);
            return;
        }
        echo json_encode(listFolders($e2, __DIR__ . '/datausers')); // $e2 = name user
        return ;
    }
    
    function listFolders($path_user, $dir) {
        $dh = scandir($dir);
        $name_user = $path_user;
        $return_data = [];
    
        if (strpos($dir, "/datausers/PERSONNAL") !== false){ // PERSONNAL found !
            if ($dir === __DIR__ . '/datausers/PERSONNAL') {  
                foreach ($dh as $item) {
                    if ($item != '.' && $item != '..') {
                        if ($item === $name_user){ // Take only a user
                            if (is_dir($dir . '/' . $item)) { // it's a folder
                                $return_data[] = array(
                                    'title' => $item,
                                    'folder' => true,
                                    'expanded'=> true,
                                    'children' => listFolders($name_user, $dir . '/' . $item, $key) 
                                );
                            } else { // it's a file
                                $return_data[] = [
                                    'title' => $item,
                                ];
                            }
                        }
                    }
                }
            }
    
            if (strpos($dir, "/datausers/PERSONNAL/" . $name_user) !== false){ // Peter found
                foreach ($dh as $item) {
                    if ($item != '.' && $item != '..') {
                        if (is_dir($dir . '/' . $item)) { // it's a folder
                            $return_data[] = array(
                                'title' => $item,
                                'folder' => true,
                                'expanded'=> true,
                                'children' => listFolders($name_user, $dir . '/' . $item, $key) 
                            );
                        } else { // it's a file
                            $return_data[] = [
                                'title' => $item,
                            ];
                        }
                    }
                }
            }
        } 
        else {
            foreach ($dh as $item) {
                if ($item != '.' && $item != '..') {
                    if (is_dir($dir . '/' . $item)) { // it's a folder
                        $return_data[] = array(
                            'title' => $item,
                            'folder' => true,
                            'expanded'=> true,
                            'children' => listFolders($name_user, $dir . '/' . $item, $key)
                        );
                    } else { // it's a file
                        $return_data[] = [
                            'title' => $item,
                        ];
                    }
                }
            }
        }
        return $return_data;
    }
    ?>