Creating filetree from full path in php

I am trying to make some kind of file tree from full path lines.

This is what I use php class:

/RootFolder/Folder1/File1.doc /RootFolder/Folder1/SubFolder1/File1.txt /RootFolder/Folder2/SubFolder1/File2.txt /RootFolder/Folder2/SubFolder1/SubSubFolder1/File4.doc 

I thought I could create such an array:

 array ( "RootFolder" => array ("Folder1" => array ("FILE" => "File1.doc" "SubFolder1" => "File1.txt" "SubFolder1" => "File2.txt" ) "Folder2" => array ( ... ) ) ) 

To get this result with <li> or in <table> :

 RootFolder/ |_ Folder1/ | |_ SubFolder1/ | | |_ File1.txt | | |_ File2.txt | |_ File1.doc | |_ Folder2/ |_ SubFolder1/ |_ SubSubFolder1/ |_ File2.txt 

I focus on this because I'm a little confused and, of course, I'm not a developer.

Is there any other way to do this? I think I will have about> 10,000 lines of file to continue when the page is loaded.

EDIT:

Actually, I have a php object that returns me arrays like this:

 Array ( [0] => Transmission\Model\File Object ( [name:protected] => RootFolder/Folder1/File1.jpg [size:protected] => 13324 [completed:protected] => 13324 [client:protected] => ) [1] => Transmission\Model\File Object ( [name:protected] => RootFolder/Folder1/File2.mp3 [size:protected] => 10383488 [completed:protected] => 10383488 [client:protected] => ) [2] ... ) 

What I want to do is to make some kind of file tree table with the name, size and status of the current file. I am based on this http://labs.abeautifulsite.net/archived/phpFileTree/demo/demo_classic.php .

Using this object, data can be obtained using $ var-> getName (), $ var-> getsize () and $ var-> isDone ().

+1
source share
2 answers

strtok save you.

 <?php $input = [ '/RootFolder/Folder1/File1.doc', '/RootFolder/Folder1/SubFolder1/File1.txt', '/RootFolder/Folder1/SubFolder1/File2.txt', '/RootFolder/Folder2/SubFolder1/File2.txt', '/RootFolder/Folder2/SubFolder1/SubSubFolder1/File4.doc', ]; function parseInput($input) { $result = array(); foreach ($input AS $path) { $prev = &$result; $s = strtok($path, '/'); while (($next = strtok('/')) !== false) { if (!isset($prev[$s])) { $prev[$s] = array(); } $prev = &$prev[$s]; $s = $next; } $prev[] = $s; unset($prev); } return $result; } var_dump(parseInput($input)); 

Output:

 array(1) { ["RootFolder"]=> array(2) { ["Folder1"]=> array(2) { [0]=> string(9) "File1.doc" ["SubFolder1"]=> array(2) { [0]=> string(9) "File1.txt" [1]=> string(9) "File2.txt" } } ["Folder2"]=> array(1) { ["SubFolder1"]=> array(2) { [0]=> string(9) "File2.txt" ["SubSubFolder1"]=> array(1) { [0]=> string(9) "File4.doc" } } } } } 

Then you can use the array as easily as you want.

+3
source

you can do this by simply following the path of your root folder to the following function:

 <?php $path = 'RootFolder'; ListFolder($path); function ListFolder($path){ //using the opendir function $dir_handle = @opendir($path) or die("Unable to open $path"); //Leave only the lastest folder name $explode = explode("/", $path); $dirname = end($explode); //display the target folder. echo ("<li>$dirname\n"); echo "<ul>\n"; while (false !== ($file = readdir($dir_handle))) { if($file!="." && $file!=".."){ if (is_dir($path."/".$file)){ //Display a list of sub folders. ListFolder($path."/".$file); }else{ //Display a list of files. echo "<li>$file</li>"; } } } echo "</ul>\n"; echo "</li>\n"; //closing the directory closedir($dir_handle); } ?> 

for more information visit the website page

0
source

Source: https://habr.com/ru/post/956662/


All Articles