phpjsondirectoryfancytree

FancyTree - How put in data-content generate php directory


I start the programming and FancyTree. And I suck at English ^^. I'm stuck and a little help wouldn't be a bad thing, thanks.

I managed to generate a PHP tree structure of my files and folders for FancyTree. So far so good. My problem is that I can't find this structure in FancyTree. When I build my PHP, I would have to put in "DATA-CONTENT" all the parent folders where the folder or file in question is located.

see my tree : my tree

maybe for more understanding : Tree example :

desired result to insert in data-content in each element :

On Github, i started my project : https://github.com/ScottMeyer48/AllForOne

Here is my PHP code :

<?php
$struct = listFolders();
echo json_encode($struct);

function listFolders($dir = __DIR__ . '/_donnees') {
    $dh = scandir($dir);
    $return = [];

    foreach ($dh as $folder) {
        if ($folder != '.' && $folder != '..') {
            if (is_dir($dir . '/' . $folder)) { // es-ce que c'est un répertoire ?
                $return[] = array(
                    'title' => $folder,
                    'folder' => true,
                    'expanded'=> true,
                    'data-content' => 'Here_Full_Path',
                    'children' => listFolders($dir . '/' . $folder, $key)
                );
            } else { // donc c'est un fichier
                $return[] = [
                    'title' => $folder,
                    'data-content' => 'Here_Full_Path',
                    // 'title' => '<a href="' . $Here_Full_Path. $folder . 'target="_self" ">' . $folder . '</a>',
                ];
            }
        }
    }
    return $return;
}
?>

Here is the result which works but incomplete:

    {"expanded":true,"key":"root_1","title":"root","children":[
    {"expanded":true,"folder":true,"key":"_2","title":"n0_A_Fol1","data":{"data-content":"Here_Full_Path"},"children":[
        {"expanded":true,"folder":true,"key":"_3","title":"n1_A_Fol1","data":{"data-content":"Here_Full_Path"},"children":[
            {"expanded":true,"folder":true,"key":"_4","title":"n2_A_Fol1","data":{"data-content":"Here_Full_Path"},"children":[
                {"expanded":true,"folder":true,"key":"_5","title":"n3_A_Fol1","data":{"data-content":"Here_Full_Path"},"children":[
                    {"key":"_6","title":"n3_A_files1.txt","data":{"data-content":"Here_Full_Path"}},
                    {"key":"_7","title":"n3_A_files2.txt","data":{"data-content":"Here_Full_Path"}}]}]},
            {"key":"_8","title":"n2_A_files.txt","data":{"data-content":"Here_Full_Path"}}]}]},
    {"expanded":true,"folder":true,"key":"_9","title":"n0_B_Fol2","data":{"data-content":"Here_Full_Path"},"children":[
        {"expanded":true,"folder":true,"key":"_10","title":"n1_B_D1","data":{"data-content":"Here_Full_Path"},"children":[
            {"key":"_11","title":"n2_B_files1.txt","data":{"data-content":"Here_Full_Path"}},
            {"key":"_12","title":"n2_B_files2.txt","data":{"data-content":"Here_Full_Path"}}]},
        {"expanded":true,"folder":true,"key":"_13","title":"n1_B_D2","data":{"data-content":"Here_Full_Path"},"children":[
            {"key":"_14","title":"n2_B_files.txt","data":{"data-content":"Here_Full_Path"}}]},
        {"key":"_15","title":"n1_B_files.txt","data":{"data-content":"Here_Full_Path"}}]}]}

I seek this result, stack all folder parent in data-content in PHP code i think. :

{"expanded":true,"key":"root_1","title":"root","children":[
    {"expanded":true,"folder":true,"key":"_2","title":"n0_A_Fol1","data":{"data-content":"/n0_A_Fol1"},"children":[
        {"expanded":true,"folder":true,"key":"_3","title":"n1_A_Fol1","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1"},"children":[
            {"expanded":true,"folder":true,"key":"_4","title":"n2_A_Fol1","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1/n2_A_Fol1"},"children":[
                {"expanded":true,"folder":true,"key":"_5","title":"n3_A_Fol1","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1/n2_A_Fol1/n3_A_Fol1"},"children":[
                    {"key":"_6","title":"n3_A_files1.txt","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1/n2_A_Fol1/n3_A_Fol1/"}},
                    {"key":"_7","title":"n3_A_files2.txt","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1/n2_A_Fol1/n3_A_Fol1/"}}]}]},
            {"key":"_8","title":"n2_A_files.txt","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1/n2_A_Fol1/"}}]}]},
    {"expanded":true,"folder":true,"key":"_9","title":"n0_B_Fol2","data":{"data-content":"/n0_B_Fol2"},"children":[
        {"expanded":true,"folder":true,"key":"_10","title":"n1_B_D1","data":{"data-content":"/n0_B_Fol2/n1_B_D1"},"children":[
            {"key":"_11","title":"n2_B_files1.txt","data":{"data-content":"/n0_B_Fol2/n1_B_D1/"}},
            {"key":"_12","title":"n2_B_files2.txt","data":{"data-content":"/n0_B_Fol2/n1_B_D1/"}}]},
        {"expanded":true,"folder":true,"key":"_13","title":"n1_B_D2","data":{"data-content":"/n0_B_Fol2/n1_B_D2"},"children":[
            {"key":"_14","title":"n2_B_files.txt","data":{"data-content":"/n0_B_Fol2/n1_B_D2/"}}]},
        {"key":"_15","title":"n1_B_files.txt","data":{"data-content":"/n0_B_Fol2/n1_B_D1/"}}]}]}

I've tried that, that's almost it. But it's too hard for me unfortunately ...

function ListFolder($Folder, $SkipFileExts, $SkipObjects) // Fonction pour lister les dossier
{
    $dir = opendir($Folder);
    while (false !== ($Current = readdir($dir))) // Boucle sur tout ce qu'il y a dans le répertoire
    {
        if ($Current !='.' && $Current != '..' && in_array($Current, $SkipObjects)===false)
        {
            if(is_dir($Folder.'/'.$Current)) // Si c'est un dossier
            {
                echo '<strong style="color: #ff0000;">'.$Current.'</strong><br>'; 

                $path_all = $Current;
                echo dirname($_SERVER['PHP_SELF']) . '/' . $path_all . '/';

                ListFolder($Folder.'/'.$Current, $SkipFileExts, $SkipObjects); // On liste les dossiers contenue dans le dossier (récursivité)
            }
            else
            {
                $FileExt = strtolower(substr(strrchr($Current ,'.'),1));
                if (in_array($FileExt, $SkipFileExts)===false) // On regarde si on dois afficher cette extension
                    echo $Current.'<br>';
            }
        }
    }
    closedir($dir); 
};

$Folder = '_donnees';
$SkipExts = array('tot', 'php', 'db');
$SkipObjects = array('UnDossier', 'UnFichier');
ListFolder($Folder, $SkipExts, $SkipObjects);

Gives this result :

n0_A_Fol1
/fancyDIV/n0_A_Fol1/n1_A_Fol1
/fancyDIV/n1_A_Fol1/n2_A_Fol1
/fancyDIV/n2_A_Fol1/n3_A_Fol1
/fancyDIV/n3_A_Fol1/n3_A_files1.txt
n3_A_files2.txt
n2_A_files.txt
n0_B_Fol2
/fancyDIV/n0_B_Fol2/n1_B_files.txt
n1_B_D1
/fancyDIV/n1_B_D1/n2_B_files1.txt
n2_B_files2.txt
n1_B_D2
/fancyDIV/n1_B_D2/n2_B_files.txt

thank you very much for all the help you can give me


after a lot of trying, I found the solution on my own :)

we can certainly do better.

<?php
$struct = listFolders();
echo json_encode($struct);

function listFolders($dir = __DIR__ . '/datausers') {
    $dh = scandir($dir);
    
    $return = [];

    foreach ($dh as $folder) {
        if ($folder != '.' && $folder != '..') {
            
            $url_site = "http://your_url";
            $Folder_data = "/datausers";
            
            if (is_dir($dir . '/' . $folder)) { // it's a folder
                
                $full_path_absolu = glob($dir . '/*');
                $full_path_relatif = str_replace ( __DIR__ . $Folder_data, "" , $full_path_absolu);
                $index_path = 0; 
                foreach ($full_path_relatif as $full_path_folder) {
                    // echo "[$index_path] => $full_path_folder </br>";
                    $index_path++;
                }

                $return[] = array(
                    'title' => $folder,
                    'data-content' => $full_path_folder,
                    'folder' => true,
                    'expanded'=> true,
                    'children' => listFolders($dir . '/' . $folder, $key)
                );
            } else { // it's a file

                $full_path_absolu = glob($dir . '/*');
                $full_path_relatif = str_replace ( __DIR__ . $Folder_data, "" , $full_path_absolu);
                $index_path = 0; 
                foreach ($full_path_relatif as $full_path_folder) {
                    // echo "[$index_path] => $full_path_folder </br>";
                    $index_path++;
                }

                $return[] = [
                    'title' => $folder,
                    'data-content' => $full_path_folder,
                    'title' => $folder,
                    // 'title' => '<a href="' . $url_site . $Folder_data . $full_path_folder . '">' . $folder . '</a>',
                ];
            }
        }
    }
    return $return;
}
?>


Solution

  • my problem is solved

    I am not building the path to creation in php. I am using fancytree to rebuild this path with this function

    click: function (e, data) { 
      var node = data.node; 
      var path = node.getPath(); 
      console.log(path); 
    },