我能够扫描通过FTP上传到服务器的文件目录
现在,我想丢弃空数组,并将路径和图像总数添加到MySQL数据库中。我将使用该路径和图像数量在HTML播放器中使用它们,并将这些图像作为视频运行。
这是我的代码:
<?php
$path = '/home/zackpc/usr/share/zoneminder/www/events/1';
// an unsorted array of dirs & files
$files_dirs = iterator_to_array( new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),RecursiveIteratorIterator::SELF_FIRST) );
echo '<html><body><pre>';
// create a new associative multi-dimensional array with dirs as keys and their files
$dirs_files = array();
foreach($files_dirs as $dir){
if(is_dir($dir) AND preg_match('/\/\.$/',$dir)){
$d = preg_replace('/\/\.$/','',$dir);
$dirs_files[$d] = array();
foreach($files_dirs as $file){
if(is_file($file) AND $d == dirname($file)){
$f = basename($file);
$dirs_files[$d][] = $f;
}
}
}
}
print_r($dirs_files);
echo '</pre></body></html>';
?>
我得到以下输出:
[/home/zackpc/usr/share/zoneminder/www/events/1/18/08/06/14] => Array
(
)
[/home/zackpc/usr/share/zoneminder/www/events/1/18/08/06] => Array
(
)
[/home/zackpc/usr/share/zoneminder/www/events/1/18/08/06/11/21/28] => Array
(
[0] => 00020-capture.jpg
[1] => 00043-capture.jpg
[2] => 00006-capture.jpg
[3] => 00008-capture.jpg
[4] => 00049-capture.jpg
[5] => 00007-capture.jpg
[6] => 00013-capture.jpg
[7] => 00025-capture.jpg
[8] => 00016-capture.jpg
[9] => 00033-capture.jpg
[10] => 00051-capture.jpg
[11] => 00014-capture.jpg
[12] => 00044-capture.jpg
[13] => 00001-capture.jpg
[14] => 00035-capture.jpg
[15] => 00039-capture.jpg
[16] => 00048-capture.jpg
[17] => 00009-capture.jpg
[18] => 00022-capture.jpg
[19] => 00034-capture.jpg
[20] => 00024-capture.jpg
[21] => 00041-capture.jpg
[22] => 00002-capture.jpg
[23] => 00029-capture.jpg
[24] => 00023-capture.jpg
[25] => 00010-capture.jpg
[26] => 00038-capture.jpg
[27] => 00003-capture.jpg
[28] => 00017-capture.jpg
[29] => 00047-capture.jpg
[30] => 00015-capture.jpg
[31] => 00012-capture.jpg
[32] => 00005-capture.jpg
)
答案 0 :(得分:0)
仅循环一次。只存储您需要的内容:路径和图像计数。
foreach($files_dirs as $file){
if (is_file($file)) {
$d = dirname($file);
if (!isset($dirs_files[$d])) {
$dirs_files[$d] = 0;
} else {
++$dirs_files[$d];
}
}
}
我必须说: