Scan current folder using PHP

情到浓时终转凉″ 提交于 2019-12-22 10:13:32

问题


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


回答1:


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

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




回答2:


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



回答3:


From the 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:


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
 }



回答6:


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



回答7:


try this

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



回答8:


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>


来源:https://stackoverflow.com/questions/1646099/scan-current-folder-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!