Fancytree - 用 php 生成树
Fancytree - Generate tree with php
我正在使用 Fancytree,我想生成一个 file/folder 树,但要排除所有不是来自当前用户的文件夹。
示例:
UPPERCASE = FOLDER
lowercase = file
我想用用户“Peter”创建一个树结构
我有几个问题:
- 我不明白
listFolders
这个函数,不能给它一个参数(比如$name_user)否则不能用
- 行 >>
print_r ('TAKE THIS'. $ Dir);
<< 显示了我想要拍摄的内容,但 foreach
行不通
- 我的代码重复了,因为我无法在函数内部创建函数...
这是我的代码:
<?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;
}
?>
好的,我找到了解决方案。
昨天我花了一天时间解决这个问题真是太疯狂了。
今天我终于找到了解决办法。谢谢 requinix 回复我。
我把我的解决方案(不完美)放在这里,但它有效。
<?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;
}
?>
否则构建一个具有不同目录级别的树结构和要获取的目录的过滤器。
如果它可以帮助某人:)
根目录
--Admin [此处PHP脚本]
--Datauser [这里取树结构]
$e2 = "name folder";
$path_array = explode('/', __DIR__); // put path to array
$path_shortcut_array = array_slice($path_array,0,(count($path_array)-1)); // rebuild array path without the last
$path_base_string = implode("/", $path_shortcut_array); // rebuild with '/'
echo json_encode(listFolders($e2, $path_base_string . '/datausers')); // $e2 = name user
function listFolders($path_user, $dir) {
$dh = scandir($dir);
$name_user = $path_user;
$return_data = [];
if (strpos($dir, "/datausers/PERSONNAL") !== false){ // PERSONNAL found !
$path_array = explode('/', __DIR__);
$path_shortcut_array = array_slice($path_array,0,(count($path_array)-1));
$path_base_string = implode("/", $path_shortcut_array);
if ($dir === $path_base_string . '/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)
);
}
}
}
}
}
if (strpos($dir, "/datausers/PERSONNAL/" . $name_user) !== false){ // user 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;
}
我正在使用 Fancytree,我想生成一个 file/folder 树,但要排除所有不是来自当前用户的文件夹。
示例:
UPPERCASE = FOLDER
lowercase = file
我想用用户“Peter”创建一个树结构
我有几个问题:
- 我不明白
listFolders
这个函数,不能给它一个参数(比如$name_user)否则不能用 - 行 >>
print_r ('TAKE THIS'. $ Dir);
<< 显示了我想要拍摄的内容,但foreach
行不通 - 我的代码重复了,因为我无法在函数内部创建函数...
这是我的代码:
<?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;
}
?>
好的,我找到了解决方案。 昨天我花了一天时间解决这个问题真是太疯狂了。 今天我终于找到了解决办法。谢谢 requinix 回复我。
我把我的解决方案(不完美)放在这里,但它有效。
<?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;
}
?>
否则构建一个具有不同目录级别的树结构和要获取的目录的过滤器。
如果它可以帮助某人:)
根目录
--Admin [此处PHP脚本]
--Datauser [这里取树结构]
$e2 = "name folder";
$path_array = explode('/', __DIR__); // put path to array
$path_shortcut_array = array_slice($path_array,0,(count($path_array)-1)); // rebuild array path without the last
$path_base_string = implode("/", $path_shortcut_array); // rebuild with '/'
echo json_encode(listFolders($e2, $path_base_string . '/datausers')); // $e2 = name user
function listFolders($path_user, $dir) {
$dh = scandir($dir);
$name_user = $path_user;
$return_data = [];
if (strpos($dir, "/datausers/PERSONNAL") !== false){ // PERSONNAL found !
$path_array = explode('/', __DIR__);
$path_shortcut_array = array_slice($path_array,0,(count($path_array)-1));
$path_base_string = implode("/", $path_shortcut_array);
if ($dir === $path_base_string . '/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)
);
}
}
}
}
}
if (strpos($dir, "/datausers/PERSONNAL/" . $name_user) !== false){ // user 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;
}