使用PHP扫描当前文件夹

时间:2009-10-29 19:44:52

标签: php filesystems directory

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

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

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

由于

8 个答案:

答案 0 :(得分:12)

$files = glob(dirname(__FILE__) . "/*.php");

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

答案 1 :(得分:5)

foreach (scandir('.') as $file)
    echo $file . "\n";

答案 2 :(得分:2)

来自PHP manual

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

答案 3 :(得分:2)

<?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)

答案 4 :(得分:1)

这取决于你对'扫描'的意思我假设你想做这样的事情:

$dir_handle = opendir(".");

 while($file = readdir($dir_handle)){
      //do stuff with $file
 }

答案 5 :(得分:0)

扫描当前文件夹

$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
    }
}

答案 6 :(得分:0)

试试这个

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

答案 7 :(得分:0)

列出文件夹中的所有图像

$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>