下面的代码只列出目录中的文件,不搜索子目录:
<?php
//path to directory to scan. i have included a wildcard for a sub directory
$directory = "/var/www/*/";
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");
$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }
//shuffle array
shuffle($imgs);
//select first 20 images in randomized array
$imgs = array_slice($imgs, 0, 1000);
//display images
foreach ($imgs as $img) {
echo $img . '<br>';
}
?>
但是如何列出所有目录中的所有特定扩展名(例如*.jpg)和
使用 PHP 的路径子目录?
最佳答案
您应该使用递归函数。 这可以帮助您: List all the files and folders in a Directory with PHP recursive function 查看答案并根据自己的需要进行更改。 (想帮助您使用自己的代码,但这与正确的方法相去甚远)。
关于php - 查找并列出 php 目录和子目录中的所有特定扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45021197/