简体   繁体   English

PHP:如何创建文件数组(也称为scandir数组)的数组

[英]PHP: how to create an array of an array of files (aka, an array of scandir)

I'd like to loop through 4 paths, and for each path store that specific path's files in an array. 我想遍历4条路径,并为每个路径将特定路径的文件存储在数组中。 The reason is, I'd like to insert each file title and file path in a database. 原因是,我想在数据库中插入每个文件标题和文​​件路径。

So: 所以:

$COMICS_path = array('./images/Comics/Charts/', './images/Comics/Life/', './images/Comics/Misc/', './images/Comics/Office/', './images/Comics/Political/');

$COMICS_files = array(scandir('./images/Comics/Charts/'), scandir('./images/Comics/Life/'), scandir('./images/Comics/Misc/'), scandir('./images/Comics/Office/'), scandir('./images/Comics/Political/'));

$COMICS_path seems to output the correct array. $COMICS_path似乎输出正确的数组。

but, $COMICS_files just outputs "ARRAY", when I'm expecting something like: 但是,当我期望类似以下内容时,$ COMICS_files仅输出“ ARRAY”:

$COMICS_files = (file1.png, file2.png, file3.png, ... file n.png)

Is this possible? 这可能吗? If not, can anyone direct me to the best way to loop through several folders and retrieve each file? 如果没有,谁能引导我找到遍历多个文件夹并检索每个文件的最佳方法?

Thanks! 谢谢!

$COMICS_files = array();
foreach($COMICS_path as $path)
    $COMICS_files = array_merge($COMICS_files, scandir($path));
var_dump($COMICS_files);

Since scandir() returns an array, you would have to merge them together into one big array. 由于scandir()返回一个数组,因此您必须将它们合并到一个大数组中。

It's outputting a 2 dimensional array because scandir returns an array. 它输出一个二维数组,因为scandir返回一个数组。

So $COMICS_files[0] will be (afile1.png, afile2.png, ...) and $COMICS_FILES[1] will be (bfile2.png, bfile2.png) 因此$ COMICS_files [0]将为(afile1.png,afile2.png,...),而$ COMICS_FILES [1]将为(bfile2.png,bfile2.png)

If you want all of these files in a single array you should do something like so 如果要将所有这些文件放在一个数组中,则应执行以下操作

$COMICS_path = array('./images/Comics/Charts/', './images/Comics/Life/', './images/Comics/Misc/', './images/Comics/Office/', './images/Comics/Political/');
$COMICS_files = array();

$COMICS_path_sz = count($COMICS_path);
for ($i = 0; $i < $COMICS_path_sz; ++$i) {
    $tmp_arr = scandir($COMICS_path[i]);
    if ($tmp_arr !== FALSE) {
        $COMICS_files = array_merge($COMICS_files, $tmp_arr);
    }
}

First of all, we need to sort out what is the "output" you are referring to. 首先,我们需要理清您所指的“输出”。 If you refer to the results of echo, then the expected result is "ARRAY". 如果您参考echo的结果,则预期结果为“ ARRAY”。

Secondly, as your "output" states, you have an array. 其次,作为“输出”状态,您有一个数组。 You need to iterate through your array and insert your records. 您需要遍历数组并插入记录。

A more elegant way to do that would be to generate all your inserts and send them at once to the database server, as the communication between your application server and database server is expensive, that is, it takes time. 一种更优雅的方法是生成所有插入并将它们立即发送到数据库服务器,因为应用程序服务器与数据库服务器之间的通信非常昂贵,也就是说,这需要时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM