How do I convert this 'folder-files' array tree into a readable format like using HTML List with dropdown for...
I am trying to unzip a zip file in order to list the content of the zip files in folder and files structure.
I got this code on here and it has given me the result below
CODE TO UNZIP AND GET THE FILE CONTENTS
<?php
$filePath = 'extract/restapi.zip';
$za = new ZipArchive();
if ($za->open($filePath) !== true) { // check for the zip archive
echo "archive doesn't exist or it's on Read-only mode ";
} else {
$Tree = $pathArray = array(); //empty arrays
for ($i = 0; $i < $za->numFiles; $i++) {
$path = $za->getNameIndex($i);
$pathBySlash = array_values(explode('/', $path));
$c = count($pathBySlash);
$temp = &$Tree;
for ($j = 0; $j < $c - 1; $j++)
if (isset($temp[$pathBySlash[$j]]))
$temp = &$temp[$pathBySlash[$j]];
else {
$temp[$pathBySlash[$j]] = array();
$temp = &$temp[$pathBySlash[$j]];
}
if (substr($path, -1) == '/')
$temp[$pathBySlash[$c - 1]] = array();
else
$temp = $pathBySlash[$c - 1];
}
$array = $Tree;
echo "<pre>";
print_r($Tree);
echo "</pre>";
RESULT OF THE CODE ABOVE
Array
(
[rest-api] => Array
(
=> Array
(
)
[0] => CHANGELOG.md
[1] => CONTRIBUTING.md
[2] => core-integration.php
[3] => extras.php
[lib] => Array
(
=> Array
(
)
[endpoints] => Array
(
=> Array
(
)
[0] => class-wp-rest-attachments-controller.php
[1] => class-wp-rest-comments-controller.php
[2] => class-wp-rest-controller.php
[3] => class-wp-rest-posts-controller.php
[4] => class-wp-rest-post-statuses-controller.php
[5] => class-wp-rest-post-types-controller.php
[6] => class-wp-rest-revisions-controller.php
[7] => class-wp-rest-settings-controller.php
[8] => class-wp-rest-taxonomies-controller.php
[9] => class-wp-rest-terms-controller.php
[10] => class-wp-rest-users-controller.php
)
[fields] => Array
(
=> Array
(
)
[0] => class-wp-rest-comment-meta-fields.php
[1] => class-wp-rest-meta-fields.php
[2] => class-wp-rest-post-meta-fields.php
[3] => class-wp-rest-term-meta-fields.php
[4] => class-wp-rest-user-meta-fields.php
)
)
[4] => license.txt
[5] => plugin.php
[6] => README.md
[7] => readme.txt
[8] => wp-api.js
[9] => wp-api.min.js
[10] => wp-api.min.map
)
)
I will be glad if there could be suggestion to a better way of doing this or providing a way to list them in folder-file structure using HTML list and dropdown for files.
php
add a comment |
I am trying to unzip a zip file in order to list the content of the zip files in folder and files structure.
I got this code on here and it has given me the result below
CODE TO UNZIP AND GET THE FILE CONTENTS
<?php
$filePath = 'extract/restapi.zip';
$za = new ZipArchive();
if ($za->open($filePath) !== true) { // check for the zip archive
echo "archive doesn't exist or it's on Read-only mode ";
} else {
$Tree = $pathArray = array(); //empty arrays
for ($i = 0; $i < $za->numFiles; $i++) {
$path = $za->getNameIndex($i);
$pathBySlash = array_values(explode('/', $path));
$c = count($pathBySlash);
$temp = &$Tree;
for ($j = 0; $j < $c - 1; $j++)
if (isset($temp[$pathBySlash[$j]]))
$temp = &$temp[$pathBySlash[$j]];
else {
$temp[$pathBySlash[$j]] = array();
$temp = &$temp[$pathBySlash[$j]];
}
if (substr($path, -1) == '/')
$temp[$pathBySlash[$c - 1]] = array();
else
$temp = $pathBySlash[$c - 1];
}
$array = $Tree;
echo "<pre>";
print_r($Tree);
echo "</pre>";
RESULT OF THE CODE ABOVE
Array
(
[rest-api] => Array
(
=> Array
(
)
[0] => CHANGELOG.md
[1] => CONTRIBUTING.md
[2] => core-integration.php
[3] => extras.php
[lib] => Array
(
=> Array
(
)
[endpoints] => Array
(
=> Array
(
)
[0] => class-wp-rest-attachments-controller.php
[1] => class-wp-rest-comments-controller.php
[2] => class-wp-rest-controller.php
[3] => class-wp-rest-posts-controller.php
[4] => class-wp-rest-post-statuses-controller.php
[5] => class-wp-rest-post-types-controller.php
[6] => class-wp-rest-revisions-controller.php
[7] => class-wp-rest-settings-controller.php
[8] => class-wp-rest-taxonomies-controller.php
[9] => class-wp-rest-terms-controller.php
[10] => class-wp-rest-users-controller.php
)
[fields] => Array
(
=> Array
(
)
[0] => class-wp-rest-comment-meta-fields.php
[1] => class-wp-rest-meta-fields.php
[2] => class-wp-rest-post-meta-fields.php
[3] => class-wp-rest-term-meta-fields.php
[4] => class-wp-rest-user-meta-fields.php
)
)
[4] => license.txt
[5] => plugin.php
[6] => README.md
[7] => readme.txt
[8] => wp-api.js
[9] => wp-api.min.js
[10] => wp-api.min.map
)
)
I will be glad if there could be suggestion to a better way of doing this or providing a way to list them in folder-file structure using HTML list and dropdown for files.
php
add a comment |
I am trying to unzip a zip file in order to list the content of the zip files in folder and files structure.
I got this code on here and it has given me the result below
CODE TO UNZIP AND GET THE FILE CONTENTS
<?php
$filePath = 'extract/restapi.zip';
$za = new ZipArchive();
if ($za->open($filePath) !== true) { // check for the zip archive
echo "archive doesn't exist or it's on Read-only mode ";
} else {
$Tree = $pathArray = array(); //empty arrays
for ($i = 0; $i < $za->numFiles; $i++) {
$path = $za->getNameIndex($i);
$pathBySlash = array_values(explode('/', $path));
$c = count($pathBySlash);
$temp = &$Tree;
for ($j = 0; $j < $c - 1; $j++)
if (isset($temp[$pathBySlash[$j]]))
$temp = &$temp[$pathBySlash[$j]];
else {
$temp[$pathBySlash[$j]] = array();
$temp = &$temp[$pathBySlash[$j]];
}
if (substr($path, -1) == '/')
$temp[$pathBySlash[$c - 1]] = array();
else
$temp = $pathBySlash[$c - 1];
}
$array = $Tree;
echo "<pre>";
print_r($Tree);
echo "</pre>";
RESULT OF THE CODE ABOVE
Array
(
[rest-api] => Array
(
=> Array
(
)
[0] => CHANGELOG.md
[1] => CONTRIBUTING.md
[2] => core-integration.php
[3] => extras.php
[lib] => Array
(
=> Array
(
)
[endpoints] => Array
(
=> Array
(
)
[0] => class-wp-rest-attachments-controller.php
[1] => class-wp-rest-comments-controller.php
[2] => class-wp-rest-controller.php
[3] => class-wp-rest-posts-controller.php
[4] => class-wp-rest-post-statuses-controller.php
[5] => class-wp-rest-post-types-controller.php
[6] => class-wp-rest-revisions-controller.php
[7] => class-wp-rest-settings-controller.php
[8] => class-wp-rest-taxonomies-controller.php
[9] => class-wp-rest-terms-controller.php
[10] => class-wp-rest-users-controller.php
)
[fields] => Array
(
=> Array
(
)
[0] => class-wp-rest-comment-meta-fields.php
[1] => class-wp-rest-meta-fields.php
[2] => class-wp-rest-post-meta-fields.php
[3] => class-wp-rest-term-meta-fields.php
[4] => class-wp-rest-user-meta-fields.php
)
)
[4] => license.txt
[5] => plugin.php
[6] => README.md
[7] => readme.txt
[8] => wp-api.js
[9] => wp-api.min.js
[10] => wp-api.min.map
)
)
I will be glad if there could be suggestion to a better way of doing this or providing a way to list them in folder-file structure using HTML list and dropdown for files.
php
I am trying to unzip a zip file in order to list the content of the zip files in folder and files structure.
I got this code on here and it has given me the result below
CODE TO UNZIP AND GET THE FILE CONTENTS
<?php
$filePath = 'extract/restapi.zip';
$za = new ZipArchive();
if ($za->open($filePath) !== true) { // check for the zip archive
echo "archive doesn't exist or it's on Read-only mode ";
} else {
$Tree = $pathArray = array(); //empty arrays
for ($i = 0; $i < $za->numFiles; $i++) {
$path = $za->getNameIndex($i);
$pathBySlash = array_values(explode('/', $path));
$c = count($pathBySlash);
$temp = &$Tree;
for ($j = 0; $j < $c - 1; $j++)
if (isset($temp[$pathBySlash[$j]]))
$temp = &$temp[$pathBySlash[$j]];
else {
$temp[$pathBySlash[$j]] = array();
$temp = &$temp[$pathBySlash[$j]];
}
if (substr($path, -1) == '/')
$temp[$pathBySlash[$c - 1]] = array();
else
$temp = $pathBySlash[$c - 1];
}
$array = $Tree;
echo "<pre>";
print_r($Tree);
echo "</pre>";
RESULT OF THE CODE ABOVE
Array
(
[rest-api] => Array
(
=> Array
(
)
[0] => CHANGELOG.md
[1] => CONTRIBUTING.md
[2] => core-integration.php
[3] => extras.php
[lib] => Array
(
=> Array
(
)
[endpoints] => Array
(
=> Array
(
)
[0] => class-wp-rest-attachments-controller.php
[1] => class-wp-rest-comments-controller.php
[2] => class-wp-rest-controller.php
[3] => class-wp-rest-posts-controller.php
[4] => class-wp-rest-post-statuses-controller.php
[5] => class-wp-rest-post-types-controller.php
[6] => class-wp-rest-revisions-controller.php
[7] => class-wp-rest-settings-controller.php
[8] => class-wp-rest-taxonomies-controller.php
[9] => class-wp-rest-terms-controller.php
[10] => class-wp-rest-users-controller.php
)
[fields] => Array
(
=> Array
(
)
[0] => class-wp-rest-comment-meta-fields.php
[1] => class-wp-rest-meta-fields.php
[2] => class-wp-rest-post-meta-fields.php
[3] => class-wp-rest-term-meta-fields.php
[4] => class-wp-rest-user-meta-fields.php
)
)
[4] => license.txt
[5] => plugin.php
[6] => README.md
[7] => readme.txt
[8] => wp-api.js
[9] => wp-api.min.js
[10] => wp-api.min.map
)
)
I will be glad if there could be suggestion to a better way of doing this or providing a way to list them in folder-file structure using HTML list and dropdown for files.
php
php
asked Nov 24 '18 at 12:13
Omolewa StephenOmolewa Stephen
17412
17412
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The easiest way to do this would to iterate the zip and echo statIndex()
like this:
<?php
// file test_zip.php
$myZip = new ZipArchive();
$myZip->open('test.zip');
for ($i=0; $i < $myZip->numFiles; $i++) {
echo $myZip->statIndex($i)['name'] . "n";
}
$myZip->close();
?>
You will get an output with folder/file structure like this:
$ php test_zip.php
test/
test/file3
test/file2
test/file1
test/folder2/
test/folder2/file2
test/folder2/file1
test/folder1/
test/folder1/file2
test/folder1/file1
Thanks, but I will need to hyperlink the files e.g (file.html) in order to read its content into an editor.
– Omolewa Stephen
Nov 24 '18 at 12:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458033%2fhow-do-i-convert-this-folder-files-array-tree-into-a-readable-format-like-usin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The easiest way to do this would to iterate the zip and echo statIndex()
like this:
<?php
// file test_zip.php
$myZip = new ZipArchive();
$myZip->open('test.zip');
for ($i=0; $i < $myZip->numFiles; $i++) {
echo $myZip->statIndex($i)['name'] . "n";
}
$myZip->close();
?>
You will get an output with folder/file structure like this:
$ php test_zip.php
test/
test/file3
test/file2
test/file1
test/folder2/
test/folder2/file2
test/folder2/file1
test/folder1/
test/folder1/file2
test/folder1/file1
Thanks, but I will need to hyperlink the files e.g (file.html) in order to read its content into an editor.
– Omolewa Stephen
Nov 24 '18 at 12:46
add a comment |
The easiest way to do this would to iterate the zip and echo statIndex()
like this:
<?php
// file test_zip.php
$myZip = new ZipArchive();
$myZip->open('test.zip');
for ($i=0; $i < $myZip->numFiles; $i++) {
echo $myZip->statIndex($i)['name'] . "n";
}
$myZip->close();
?>
You will get an output with folder/file structure like this:
$ php test_zip.php
test/
test/file3
test/file2
test/file1
test/folder2/
test/folder2/file2
test/folder2/file1
test/folder1/
test/folder1/file2
test/folder1/file1
Thanks, but I will need to hyperlink the files e.g (file.html) in order to read its content into an editor.
– Omolewa Stephen
Nov 24 '18 at 12:46
add a comment |
The easiest way to do this would to iterate the zip and echo statIndex()
like this:
<?php
// file test_zip.php
$myZip = new ZipArchive();
$myZip->open('test.zip');
for ($i=0; $i < $myZip->numFiles; $i++) {
echo $myZip->statIndex($i)['name'] . "n";
}
$myZip->close();
?>
You will get an output with folder/file structure like this:
$ php test_zip.php
test/
test/file3
test/file2
test/file1
test/folder2/
test/folder2/file2
test/folder2/file1
test/folder1/
test/folder1/file2
test/folder1/file1
The easiest way to do this would to iterate the zip and echo statIndex()
like this:
<?php
// file test_zip.php
$myZip = new ZipArchive();
$myZip->open('test.zip');
for ($i=0; $i < $myZip->numFiles; $i++) {
echo $myZip->statIndex($i)['name'] . "n";
}
$myZip->close();
?>
You will get an output with folder/file structure like this:
$ php test_zip.php
test/
test/file3
test/file2
test/file1
test/folder2/
test/folder2/file2
test/folder2/file1
test/folder1/
test/folder1/file2
test/folder1/file1
answered Nov 24 '18 at 12:41
digijaydigijay
5861715
5861715
Thanks, but I will need to hyperlink the files e.g (file.html) in order to read its content into an editor.
– Omolewa Stephen
Nov 24 '18 at 12:46
add a comment |
Thanks, but I will need to hyperlink the files e.g (file.html) in order to read its content into an editor.
– Omolewa Stephen
Nov 24 '18 at 12:46
Thanks, but I will need to hyperlink the files e.g (file.html) in order to read its content into an editor.
– Omolewa Stephen
Nov 24 '18 at 12:46
Thanks, but I will need to hyperlink the files e.g (file.html) in order to read its content into an editor.
– Omolewa Stephen
Nov 24 '18 at 12:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458033%2fhow-do-i-convert-this-folder-files-array-tree-into-a-readable-format-like-usin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown