2015-05-08 65 views
0

我在我的網絡上爲我的電影運行Apache服務器。我最近發現.HTACCESS的威力,並決定嘗試使用它來使我的所有目錄看起來不錯,而不是默認索引。問題是我使用一個基本的PHP腳本,它只獲取當前目錄,而不是像我想要的那樣從所有電影目錄中獲取。我這樣做的原因是,我不必將索引文件複製/粘貼到我的每個540文件夾中。我不確定是否有什麼我做錯了或我可以使用一些不同的PHP。任何幫助讚賞。Apache HTACCESS目錄/錯誤的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"); 
    ?> 

回答

0

這是一個recoursive目錄讀取器類:

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 
     ) 

) 
+0

很抱歉,如果我是小白,但我擺脫了PHP,並加入 在體內,該腳本未加載 –

+0

我想通了,但我得到一個錯誤,它無法找到類。致命錯誤:在'我的PHP文件*'中找不到類'DirectoryReader'。我試圖把這個類放在php文件中,並將它放在它自己的.js文件中,並使用腳本src,但仍然是相同的錯誤。 –

+0

我得到了腳本工作,我現在該做什麼!它顯示每個目錄中的每個文件。不知道該從哪裏出發。 –