php - 查找并列出 php 目录和子目录中的所有特定扩展名

标签 php

下面的代码只列出目录中的文件,不搜索子目录:

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

相关文章:

php - 无法在 codeigniter 3.0 中获取 session_id

php - 从与第一条记录匹配的表中选择第二条记录

php - preg_match 从其他站点选择 URL

php - 找不到 "GET/assets/css/..."的路由

php - Eclipse PDT 中的 HTML 所见即所得编辑器

JavaScript、AJAX、PHP : can't set field values after getting json string

php - iOS - 了解 PHP JSON 文件何时更新

php - mysqli 连接不适用于循环 DNS

php - 返回给定 id 的所有行值

php - 从 mysql 表创建复选框(表)