【问题标题】:Scan current folder using PHP使用 PHP 扫描当前文件夹
【发布时间】:2010-12-11 09:25:13
【问题描述】:

我有一个这样的文件夹结构:

/articles
     .index.php
     .second.php
     .third.php
     .fourth.php

如果我在 second.php 中编写代码,如何扫描当前文件夹(文章)?

谢谢

【问题讨论】:

  • 请注意:scandir('.') 之类的句点指的是当前目录

标签: php filesystems directory


【解决方案1】:
$files = glob(dirname(__FILE__) . "/*.php");

http://php.net/manual/en/function.glob.php

【讨论】:

    【解决方案2】:

    这取决于你所说的“扫描”是什么意思,我假设你想做这样的事情:

    $dir_handle = opendir(".");
    
     while($file = readdir($dir_handle)){
          //do stuff with $file
     }
    

    【讨论】:

      【解决方案3】:

      来自 PHP manual:

      $dir = new DirectoryIterator(dirname($path));
      foreach ($dir as $fileinfo) {
          if (!$fileinfo->isDot()) {
              var_dump($fileinfo->getFilename());
          }
      }
      

      【讨论】:

        【解决方案4】:
        <?php
        
        $path = new DirectoryIterator('/articles');
        
        foreach ($path as $file) {
            echo $file->getFilename() . "\t";
            echo $file->getSize() . "\t";
            echo $file->getOwner() . "\t";
            echo $file->getMTime() . "\n";
        }
        
        ?>
        

        From The Standard PHP Library (SPL)

        【讨论】:

          【解决方案5】:
          foreach (scandir('.') as $file)
              echo $file . "\n";
          

          【讨论】:

            【解决方案6】:

            扫描当前文件夹

            $zip = new ZipArchive();
            $x = $zip->open($filepath);
            if ($x === true) {
              $zip->extractTo($uploadPath); // place in the directory
              $zip->close();
            
              $fileArray = scandir($uploadPath);
                unlink($filepath);
            }
             foreach ($fileArray as $file) {
                if ('.' === $file || '..' === $file) 
                continue;
                if (!is_dir("$file")){
                   //do stuff with $file
                }
            }
            

            【讨论】:

              【解决方案7】:

              试试这个

                 $dir = glob(dirname(__FILE__));
                 $directory = array_diff(scandir($dir[0]), array('..', '.'));
                 print_r($directory);
              

              【讨论】:

                【解决方案8】:

                列出文件夹内的所有图片

                $dir = glob(dirname(__FILE__));
                
                $path = $dir[0].'\\images';
                
                $imagePaths = array_diff( scandir( $path ), array('.', '..', 'Thumbs.db')); 
                ?>
                <ul style="overflow-y: auto; max-height: 80vh;">
                <?php
                
                    foreach($imagePaths as $imagePath)
                    {
                ?>
                    <li><?php echo '<img class="pagina" src="images/'.$imagePath.'" />'; ?></li>
                <?php
                    }
                        ?>
                </ul>
                

                【讨论】:

                  猜你喜欢
                  • 2018-07-17
                  • 2013-06-14
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-01-24
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多