Apache HTACCESS目录/错误的PHP代码?

时间:2015-05-08 08:31:47

标签: php apache .htaccess directory

我在网络上为我的电影运行Apache服务器。我最近发现了.HTACCESS的强大功能,并决定尝试使用它来使我的所有目录看起来都不错,而不是默认索引。问题是我使用的基本PHP脚本只获取当前目录,而不是像我想要的那样从所有电影目录中获取。我这样做的原因是我不必将索引文件复制/粘贴到我的540个文件夹中。我不确定我做错了什么或者我可以使用一些不同的PHP。任何帮助表示赞赏。

这是我的.HTACCESS

# DISABLE DIRECTORY VIEWS
Options +Indexes

# STRONG HTACCESS PROTECTION
<Files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
</Files>

# DIRECTORY CUSTOMIZATION
<IfModule mod_autoindex.c>

</IfModule>

# SET INDEX OPTIONS
IndexOptions IgnoreCase FancyIndexing FoldersFirst NameWidth=70    DescriptionWidth=* VersionSort SuppressHTMLPreamble IconWidth=40 SuppressDescription SuppressLastModified SuppressSize HTMLTable 

# SPECIFY HEADER FILE
HeaderName /header.php

# SPECIFY FOOTER FILE
ReadmeName /footer.html

# IGNORE THESE FILES
IndexIgnore header.html index.php *.nfo *.txt *.srt footer.html favicon.ico .htaccess .ftpquota .DS_Store icons *.log *,v *,t .??* *~ *# Thumbs.db

DirectoryIndex index.html index.php

这是我的PHP:

<?php

  // open this directory 
  $myDirectory = opendir(".");

  // get each entry
  while($entryName = readdir($myDirectory)) {
          $dirArray[] = $entryName;
  }

  // close directory
  closedir($myDirectory);

  //      count elements in array
  $indexCount     = count($dirArray);

  // sort 'em
  sort($dirArray);

  // print 'em

  print("<TABLE border=1 cellpadding=10 cellspacing=0 class='main'>");
  Print ("<td><b>$indexCount movies</b></td>");

  //print("<TR><TH>Filename</TH></TR>\n");

  // loop through the array of files and print them all
  for($index=0; $index < $indexCount; $index++) {
          if (substr("$dirArray[$index]", 0, 1) != "."){// don't list hidden files
                  print("<TR><TD width='50%'><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
                  print("</TR>\n");
          }
          }
  print("</TABLE>\n");
  ?>

1 个答案:

答案 0 :(得分:0)

这是一个递归目录读者类:

class DirectoryReader {

      public static function readFolderRecursive($folder, &$res) {
          if(substr($folder,-1) == "/") {
              $folder = substr($folder,0,-1);
          }
          $data = self::readFolder($folder);
          foreach($data['files'] as $k => $f) {
              $res[] = array('type'=>'file','path'=>$folder."/".$f, 'filename'=>$f);
          }
          foreach($data['folder'] as $k => $f) {
              $tmp = $folder."/".$f;
              $res[] = array('type'=>'folder','path'=>$folder."/".$f);
              self::readFolderRecursive($tmp, $res);
          }
      }

      public static function readFolder($folder) {
          $files = array();
          $folders = array();

          if(is_dir($folder) ) {
              if(substr($folder,-1) == "/") {
                  $folder = substr($folder,0,-1);
              }
              $handle = opendir($folder);
              while ($file = readdir ($handle)) {
                  if($file != "." && $file != "..") {
                      if(is_dir($folder."/".$file)) {
                          $folders[] = $file;
                      } else {
                          $files[]= $file;
                      }
                  }
              }
              closedir($handle);
          }
          return array("folder" => $folders, "files" => $files);
      }

}

$data = array();
DirectoryReader::readFolderRecursive(".",$data);

echo "<pre>";
print_r($data);
echo "</pre>";

它将给定文件夹的所有子文件夹和文件读入数组。您应该能够自己格式化生成的$ data数组。

因此,您可以生成一个页面,您可以在其中查看所有子文件夹的所有内容。如果要打开另一个目录,则不需要重新加载php文件。您可以使用纯JavaScript来解决此问题。

示例结果如下所示:

Array
(
    [0] => Array
        (
            [type] => file
            [path] => ./test.php
            [filename] => test.php
        )

    [1] => Array
        (
            [type] => folder
            [path] => ./example_folder_2
        )

    [2] => Array
        (
            [type] => file
            [path] => ./example_folder_2/file3.txt
            [filename] => file3.txt
        )

    [3] => Array
        (
            [type] => folder
            [path] => ./example_folder_1
        )

    [4] => Array
        (
            [type] => file
            [path] => ./example_folder_1/file1.txt
            [filename] => file1.txt
        )

    [5] => Array
        (
            [type] => folder
            [path] => ./example_folder_1/example_subfolder
        )

    [6] => Array
        (
            [type] => file
            [path] => ./example_folder_1/example_subfolder/file2.txt
            [filename] => file2.txt
        )

)