简体   繁体   English

在jQuery文件树中隐藏文件和文件夹

[英]Hide files & folders in jQuery File Tree

I am using jQuery File Tree to display a directory listing, toghether with the standard PHP connector provided with the File Tree code. 我正在使用jQuery File Tree来显示目录列表,以及File Tree代码随附的标准PHP连接器。

Everything works fine, but I need to filter the listing to avoid the inclusion of hiden files and not desired folders. 一切正常,但是我需要过滤列表以避免包含隐藏文件而不是不需要的文件夹。 My skills in PHP or JS doesn't allow me to go futher than pasting here the code, with the hope I can get some extra lines to hide unwanted files according to certain pattern/s. 我在PHP或JS方面的技能不允许我比在此处粘贴代码更进一步,希望我可以根据某些模式获得一些额外的行来隐藏不需要的文件。

Thanks! 谢谢!

The HTML code: HTML代码:

<html>
<head>
<link rel="stylesheet" href="../../js/ft/jqueryFileTree.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="../../js/ft/jqueryFileTree.js"></script>
<script type="text/javascript">
function openFile(file) {
    window.location = file;
}
$(document).ready (function() {
$('.filetree').fileTree({
root: '../../../est/dir/',
script: '../../js/ft/connectors/jqueryFileTree.php',
function(file) {
window.open(file);
});
});
</script>

</head>
<body>
   <div class="filetree"></div>
</body>
</html>

And the PHP code: 和PHP代码:

<?php
$_POST['dir'] = urldecode($_POST['dir']);

if( file_exists($_POST['dir']) ) {
   $files = scandir($_POST['dir']);
   natcasesort($files);
   if( count($files) > 2 ) { // The 2 accounts for . and .. 
      echo "<ul class=\"jqueryFileTree\" style=\"display: none;\">";
      // All dirs
      foreach( $files as $file ) {
         if( file_exists($_POST['dir'] . $file) && $file != '.' && $file != '..' && is_dir($_POST['dir'] . $file) ) {
            echo "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "/\">" . htmlentities($file) . "</a></li>";
         }
      }
      // All files
      foreach( $files as $file ) {
         if( file_exists($_POST['dir'] . $file) && $file != '.' && $file != '..' && !is_dir($_POST['dir'] . $file) ) {
            $ext = preg_replace('/^.*\./', '', $file);
            echo "<li class=\"file ext_$ext\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "\">" . htmlentities($file) . "</a></li>";
         }
      }
      echo "</ul>"; 
   }
}

?>

PS: original source comming from here PS:原始资料来自这里

I am not familiar with jQuery File Tree but I believe that the key to your issue lies inside the loops. 我对jQuery File Tree不熟悉,但我相信问题的关键就在于循环。

The only thing you need to do is create a blacklist, an array with the name of the folders/files you dont want to show. 您唯一需要做的就是创建一个黑名单,一个黑名单,其中包含您不想显示的文件夹/文件的名称。

$blacklist = array('namefile1', 'namefolder1', 'namefile2');

And then implement a check inside the loop, so that it skips the name if the file/folder name matches one that is inside the blacklist (case sensitive). 然后在循环内实施检查,以便如果文件/文件夹名称与黑名单内的文件/文件夹名称匹配(区分大小写),则跳过该名称。

foreach( $files as $file ) 
{
     if (in_array($file, $blacklist))
         continue;

     .... the rest of the code ...
     .... goes here ..............
}

That is basically what you need to do. 这基本上就是您需要做的。 You could also use regular expressions and the preg_match function, to make it more flexible.. 您还可以使用正则表达式和preg_match函数,使其更加灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM