[英]How to copy/move images from array to a folder in php
我有两个名为“ avatars”和“ small_avatars”的文件夹。 我想做的是使用数组将这些文件夹(都有图像)的内容相互比较,就像这样(请参见下面的代码),但我认为它不起作用。 有人可以检查一下并指出需要进行哪些更改才能使其正常工作吗? 是否需要定义要保存到阵列中的文件的数据类型?
$dirBig= $root.AVATAR_DIRECTORY;
$dirSmall= $root.SMALL_AVATAR_DIRECTORY;
$imagesBig = array("image/gif","image/jpg","image/jpeg","image/png");
$imagesSmall = array("image/gif","image/jpg","image/jpeg","image/png");
// check if avatar directory exists then copy the names
// of the files to an array
if (is_dir($dirBig)){
// for debug purposes
echo ("$dirBig on dir </br></br>");
}else{
echo ("dir not found\n" );
}
if ($isot=opendir($dirBig)){
while ((false !== $file = readdir($isot))):
if ($file[0] == ".")continue; {
echo ("Filename in avatars : $file </br>");
$imagesBig[]=$file;
}
endwhile;
closedir($isot);
}
// check if small avatar directory exists then copy the names
// of the files to an array
if (is_dir($dirSmall)) {
echo ("</br>".SMALL_AVATAR_DIRECTORY. " on dir </br></br>");
}else{
echo("dir not found \n");
}
if ($pienet=opendir($dirSmall)) {
while ((false !== $file = readdir($pienet))) :
if ($file[0] == ".") continue;{
echo ("Filename in small avatars: $file</br>");
$imagesSmall[] = $file;
}
endwhile;
closedir($pienet);
}
//compare the two arrays with each other
$comp_result= array_diff($imagesBig, $imagesSmall);`
两个数组相互比较的结果包含“ small_avatar”文件夹中不存在的文件的名称。 该文件名没有扩展名,如:” JPG,.png等,我想实现的下一件事是尝试这种从‘$ comp_result’这些图像复制到‘small_avatar’文件夹: 编辑替换前使用此foreach进行for循环。现在没有错误,但图像仍未从数组复制到文件夹... :(
$a = $comp_result;
foreach ($a as $img){
if (move_uploaded_file($img, $dirSmall)){
$imagetools->resizeImage($dirSmall, NULL, NULL, NULL, THREAD_AVATAR_MAX_WIDTH, THREAD_AVATAR_MAX_HEIGHT, AVATAR_KEEP_IMAGE_ASPECT_RATIO);
}else{
echo("Copying file ".$img." failed to dir ".$dirSmall." !</br>");
}
}
但这是行不通的。 我收到如下错误消息:
如果有人可以帮助我使此功能正常工作,我将不胜感激。 我已经在Google搜索了几天了。
我已经尝试过glob()
,但是没有给我任何值...
先感谢您! :)
我无法解决所有错误,但是我认为第一个错误(未定义的偏移量)是因为这一行
(false !== $file = readdir($isot))
需要写成
(false !== ($file = readdir($isot)))
由于运算符的优先级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.