【问题标题】:List Directories like wamp page in PHP列出目录,如 PHP 中的 wamp 页面
【发布时间】:2012-02-07 13:34:58
【问题描述】:

我正在寻找一种方法来列出目录中每个文件夹的名称及其在 PHP 中的路径

谢谢

【问题讨论】:

  • Google“列出目录中每个文件夹的名称及其在 PHP 中的路径”。
  • 你可能指的是index.html吗?

标签: php html directory


【解决方案1】:

您所指的不是 WAMPP 的页面,它是在任何(如果不是大多数)Web 服务器上显示文件和文件夹的默认设置...这通常由 Web 服务器配置关闭,或者 @ 987654321@

您正在寻找一些 PHP 代码来做类似的事情,以下 PHP 函数是您需要研究的,阅读页面并查看示例以了解如何使用它们...不要忽略“警告" 或来自 php.net 的这些页面上的“重要”消息:

  • opendir - 创建用于读取的目录句柄
  • readdir - 读取目录中的文件/文件夹
  • rmdir - 删除文件夹(必须为空)
  • mkdir - 创建文件夹

这是一个例子:

<?php
$folder = "myfolder";
if ($dhandle = opendir($folder)) {

  while ($file = readdir($dhandle)) {

  // Ignore . and ..
  if ($file<>'.' && $file<>'..')

  // if it's a folder, echo [F]

  if (is_dir("$folder/$file")) echo "[F] $file<br>"; else
  echo "$file<br>";

  }
closedir($dhandle);
}
?>

重要 请记住,在 Linux 操作系统上,您的 Apache/PHP 必须有权访问相关文件夹,然后才能写入/删除文件和文件夹……阅读chmodchownchgrp

【讨论】:

    【解决方案2】:

    使用以下函数获取文件/文件夹的路径

    <?php 
    
    function getDirectory( $path = '.', $level = 0 ){ 
    
        $ignore = array( 'cgi-bin', '.', '..' ); 
        // Directories to ignore when listing output. Many hosts 
        // will deny PHP access to the cgi-bin. 
    
        $dh = @opendir( $path ); 
        // Open the directory to the handle $dh 
    
        while( false !== ( $file = readdir( $dh ) ) ){ 
        // Loop through the directory 
    
            if( !in_array( $file, $ignore ) ){ 
            // Check that this file is not to be ignored 
    
                $spaces = str_repeat( '&nbsp;', ( $level * 4 ) ); 
                // Just to add spacing to the list, to better 
                // show the directory tree. 
    
                if( is_dir( "$path/$file" ) ){ 
                // Its a directory, so we need to keep reading down... 
    
                    echo "<strong>$spaces $file</strong><br />"; 
                    getDirectory( "$path/$file", ($level+1) ); 
                    // Re-call this same function but on a new directory. 
                    // this is what makes function recursive. 
    
                } else { 
    
                    echo "$spaces $file<br />"; 
                    // Just print out the filename 
    
                } 
    
            } 
    
        } 
    
        closedir( $dh ); 
        // Close the directory handle 
    
    } 
    getDirectory( "." ); 
    ?>
    

    【讨论】:

      【解决方案3】:

      这个问题有一个简单的解决方案:(如果你只使用linux)

      您想列出目录中每个文件夹的名称及其在 PHP 中的路径。

      你可以使用

       find 
      

      命令与 PHP 的结合

      exec();
      

      功能

      下面的sn-p显示了这个

      <?php
          $startdir = "Some Directory" ; // the start directory whose sub directories along with path is needed
          exec("find  " . $startdir . " -type d " , $directories); // executes the command and stores the result in array $directory line by line
          while(list($index,$dir) = each($directories) ) {
              echo $dir."<br/>"; //lists directories one by one 
          }
      ?>
      

      脚注:

      命令,

      find dirname -type d
      

      列出文件夹startdir下的所有目录和子目录

      【讨论】:

        【解决方案4】:

        这是一个php代码,将它保存为index.php并放在你的web根目录中。

        <?php
        $pngFolder = <<< EOFILE
        iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAA3NCSVQICAjb4U/gAAABhlBMVEX//v7//v3///7//fr//fj+/v3//fb+/fT+/Pf//PX+/Pb+/PP+/PL+/PH+/PD+++/+++7++u/9+vL9+vH79+r79+n79uj89tj89Nf889D88sj78sz78sr58N3u7u7u7ev777j67bL67Kv46sHt6uP26cns6d356aP56aD56Jv45pT45pP45ZD45I324av344r344T14J734oT34YD13pD24Hv03af13pP233X025303JL23nX23nHz2pX23Gvn2a7122fz2I3122T12mLz14Xv1JPy1YD12Vz02Fvy1H7v04T011Py03j011b01k7v0n/x0nHz1Ejv0Hnuz3Xx0Gvz00buzofz00Pxz2juz3Hy0TrmznzmzoHy0Djqy2vtymnxzS3xzi/kyG3jyG7wyyXkwJjpwHLiw2Liw2HhwmDdvlXevVPduVThsX7btDrbsj/gq3DbsDzbrT7brDvaqzjapjrbpTraojnboTrbmzrbmjrbl0Tbljrakz3ajzzZjTfZijLZiTJdVmhqAAAAgnRSTlP///////////////////////////////////////8A////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////9XzUpQAAAAlwSFlzAAALEgAACxIB0t1+/AAAAB90RVh0U29mdHdhcmUATWFjcm9tZWRpYSBGaXJld29ya3MgOLVo0ngAAACqSURBVBiVY5BDAwxECGRlpgNBtpoKCMjLM8jnsYKASFJycnJ0tD1QRT6HromhHj8YMOcABYqEzc3d4uO9vIKCIkULgQIlYq5haao8YMBUDBQoZWIBAnFtAwsHD4kyoEA5l5SCkqa+qZ27X7hkBVCgUkhRXcvI2sk3MCpRugooUCOooWNs4+wdGpuQIlMDFKiWNbO0dXTx9AwICVGuBQqkFtQ1wEB9LhGeAwDSdzMEmZfC0wAAAABJRU5ErkJggg==
        EOFILE;
        
        if (isset($_GET['img']))
        {
               header("Content-type: image/png");
                echo base64_decode($pngFolder);
                exit();
        }
        
        
        $projectsListIgnore = array ('.','..'); 
        $handle=opendir(".");
        $projectContents = '';
        while ($file = readdir($handle)) 
        {
            if (is_dir($file) && !in_array($file,$projectsListIgnore)) 
            {       
                $projectContents .= '<li><a href="'.$file.'">'.$file.'</a></li>';
            }
        }
        closedir($handle);
        
        ?>
        <ul class="projects">
        <?php $projectContents ?>
        </ul>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-07
          • 1970-01-01
          • 2014-10-24
          • 1970-01-01
          • 1970-01-01
          • 2014-04-26
          • 2015-01-09
          相关资源
          最近更新 更多