我在Stackover上寻找了一个类似的问题,但找不到解决方案。
我正在尝试编写一个脚本,查看两个目录的内容,以确定是否可以在这两个目录中找到文件名匹配。如果找到匹配项,则将匹配的文件名写入数组。
我要做的第一件事是使用“"scandir”“从第一个目录创建一个数据数组。在”foreeach“循环中,从”scandir“结果中遍历该数组,并使用变量"$image1”执行"file_exists“以在秒目录"allimages/boardsclean”中查找匹配项。如果找到匹配项,则将文件名写入"$found_images“数组。
测试"$found_images“数组的结果,我没有看到预期的结果。
有没有人能看到我哪里出了问题?
$c1 = 0;
$c2 = 0;
$scan = scandir('allimages/temp1');
$found_images = array();
foreach ($scan as $image1) {
if (file_exists('allimages/temp1/'.$image1) && ('allimages/temp2/'.$image1)) {
echo "file match in Scan $image1</br>";
$found_images[] = 'allimages/adminclean/'. $image1;
$c1++;
}
}
echo $c1."</br>";
foreach ($found_images as $image3) {
echo "file match $image3 </br>";
$c2++;
}
echo $c2."</br>";发布于 2021-02-03 07:01:10
首先,您不需要测试scandir中的文件,因为…它已经在那里了,并且被返回了。其次,您不需要测试第二个目录中的那个。您需要:
if(file_exists('allimages/temp2/'.$image1)) {但是,只需扫描这两个目录并计算返回的数组的交集,就可以得到两个目录共有的文件。它就像这样简单:
$found = array_intersect(scandir('allimages/temp1'), scandir('allimages/temp2'));然后,您可以根据需要筛选出目录,并在阵列中或需要时添加allimages/adminclean/。
https://stackoverflow.com/questions/66018411
复制相似问题