使用 PHP 扫描当前文件夹

发布于 2024-08-09 13:32:16 字数 181 浏览 3 评论 0原文

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

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

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

谢谢

I have a folder structure like this:

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

If I'm writing my code in second.php, how can I scan the current folder(articles)?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

慕烟庭风 2024-08-16 13:32:16
$files = glob(dirname(__FILE__) . "/*.php");

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

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

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

季末如歌 2024-08-16 13:32:16
foreach (scandir('.') as $file)
    echo $file . "\n";
foreach (scandir('.') as $file)
    echo $file . "\n";
很酷不放纵 2024-08-16 13:32:16

来自 PHP 手册

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

From the PHP manual:

$dir = new DirectoryIterator(dirname($path));
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
        var_dump($fileinfo->getFilename());
    }
}
ゃ懵逼小萝莉 2024-08-16 13:32:16
<?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)

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

秉烛思 2024-08-16 13:32:16

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

$dir_handle = opendir(".");

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

It depends on what you mean by 'scan' I'm assuming you want to do something like this:

$dir_handle = opendir(".");

 while($file = readdir($dir_handle)){
      //do stuff with $file
 }
我只土不豪 2024-08-16 13:32:16

试试这个

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

try this

   $dir = glob(dirname(__FILE__));
   $directory = array_diff(scandir($dir[0]), array('..', '.'));
   print_r($directory);
烟织青萝梦 2024-08-16 13:32:16

扫描当前文件夹

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

Scan current folder

$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
    }
}
最终幸福 2024-08-16 13:32:16

列出文件夹内的所有图像

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

List all images inside a folder

$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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文