使用PHP扫描当前文件夹

bq9c1y66  于 2023-01-01  发布在  PHP
关注(0)|答案(8)|浏览(197)

我的文件夹结构如下所示:

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

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

d5vmydt9

d5vmydt92#

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

lnlaulya3#

从PHP manual

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

v1l68za44#

<?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";
}

?>

来自标准PHP库(SPL)

wwwo4jvm

wwwo4jvm5#

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

$dir_handle = opendir(".");

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

3wabscal6#

试试这个

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

acruukt97#

扫描当前文件夹

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

chhqkbe18#

列出文件夹中的所有图像

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

相关问题