开发者

Scan current folder using 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开发者_StackOverflow folder(articles)?

Thanks


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

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


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


From the PHP manual:

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


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


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
 }


try this

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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜