PHP 从目录中获取文件列表(在 Moodle 中)

发布于 2024-08-23 17:05:52 字数 149 浏览 3 评论 0原文

我想打开一个目录并读取里面的所有文件并将它们放入一个数组中,到目前为止我已经:

$imagesdir = $CFG->dataroot.'/1/themeimages/';

这给了我该目录的路径,下一步是什么?

I want to open a directory and read all the files inside and put them into an array, so far I have:

$imagesdir = $CFG->dataroot.'/1/themeimages/';

Which gives me the path to the directory, whats the next step?

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

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

发布评论

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

评论(5

梦年海沫深 2024-08-30 17:05:52

由于从您的标签列表中我假设您正在使用 Moodle,因此您可以简单地使用:

function get_directory_list($rootdir, $excludefiles='', $descend=true, $getdirs=false, $getfiles=true)

该函数包含在 moodlelib.php 中。

从文档中读取:

  • 返回一个数组,其中包含所有
    * 所有子目录中的文件名,
    相对于给定的 rootdir。

有关可选参数的更多信息,请参阅官方文档。

filelib.php 中还提供了读取文件内容的函数。

Since from your tag list I assume you're using Moodle, you can simply use the:

function get_directory_list($rootdir, $excludefiles='', $descend=true, $getdirs=false, $getfiles=true)

The function is contained in moodlelib.php.

Reading from the doc:

  • Returns an array with all the
    filenames in * all subdirectories,
    relative to the given rootdir.

Please refer to the official doc for more information about the optional parameters.

Functions for reading content of files are also available in filelib.php.

冷夜 2024-08-30 17:05:52

解决方案是使用 opendir + readdir + linedir (引用第一页的示例)

$imagesdir = $CFG->dataroot.'/1/themeimages/';
if ($handle = opendir($imagesdir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "$file\n";
        }
    }
    closedir($handle);
}

Another solution would be to use [the `[DirectoryIterator` class][4] ; quoting the example from][4] the `__construct` page :

$imagesdir = $CFG->dataroot.'/1/themeimages/';
$dir = new DirectoryIterator($imagesdir);
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
        var_dump($fileinfo->getFilename());
    }
}

Of course, in each case, instead of just echoing or dumping the name of the file, you'd have to put it into an array.

这意味着在循环之前初始化数组:

$list_files = array();

并且,在循环内部,使用类似于这两行之一的内容,具体取决于您选择的解决方案:

$list_files[] = $file;
$list_files[] = $fileinfo->getFilename();

A solution would be to use opendir + readdir + closedir (quoting an example from the first page) :

$imagesdir = $CFG->dataroot.'/1/themeimages/';
if ($handle = opendir($imagesdir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "$file\n";
        }
    }
    closedir($handle);
}

Another solution would be to use [the `[DirectoryIterator` class][4] ; quoting the example from][4] the `__construct` page :

$imagesdir = $CFG->dataroot.'/1/themeimages/';
$dir = new DirectoryIterator($imagesdir);
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
        var_dump($fileinfo->getFilename());
    }
}

Of course, in each case, instead of just echoing or dumping the name of the file, you'd have to put it into an array.

This would mean initializing the array before the loop :

$list_files = array();

And, inside the loop, use something like one of those two lines, depending on which solution you choose :

$list_files[] = $file;
$list_files[] = $fileinfo->getFilename();
顾挽 2024-08-30 17:05:52

只需使用内置函数scandir

从目录返回文件和目录的数组。

所以你可以这样使用它:

$array = scandir($imagesdir);

当然你也可以使用 DirectoryIterator,但这要简单得多。

您还可以剥离点文件:

$array = array_diff(scandir($imagesdir), array('.', '..'));

Just use the built in function scandir:

Returns an array of files and directories from the directory .

So you would use it like this:

$array = scandir($imagesdir);

Of course you can use DirectoryIterator too, but this is much simplier.

You can also strip dot files:

$array = array_diff(scandir($imagesdir), array('.', '..'));
九命猫 2024-08-30 17:05:52

对于更加面向对象的方法,请使用 DirectoryIterator 类。

$images = array();
$imagesdir = $CFG->dataroot.'/1/themeimages/';
foreach (new DirectoryIterator($imagesdir) as $file) {
   if($file->isDot()) continue;
   $images[] = $file;
}

For a more OO approach use the DirectoryIterator class.

$images = array();
$imagesdir = $CFG->dataroot.'/1/themeimages/';
foreach (new DirectoryIterator($imagesdir) as $file) {
   if($file->isDot()) continue;
   $images[] = $file;
}
一个人练习一个人 2024-08-30 17:05:52

如果您稍后还需要过滤器,这是最短的解决方案:

$imagesdir = $CFG->dataroot.'/1/themeimages/*.*';
foreach (glob($imagesdir) as $file)
  array_push($files,$file);

php 自动排除 ...

如果您不需要所有文件,您也可以指定自己的掩码,如上*.*
php 还会自动为您创建 $files 数组。

最后一行也可以是:

$files[]=$file;

here's the shortest solution if you also need a filter later on:

$imagesdir = $CFG->dataroot.'/1/themeimages/*.*';
foreach (glob($imagesdir) as $file)
  array_push($files,$file);

php automatically excludes . and ..

you can also specify your own mask if you don't need ALL files, as shown in above *.*
php also automatically creates the $files array for you.

the last line can also be:

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