如何将图像从数组复制/移动到php中的文件夹


How to copy/move images from array to a folder in php

我有两个名为"avatars"answers"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"文件夹:edit用这个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>");
  }
  }

但它不起作用。我收到错误消息,例如:

  • 警告:copy():文件名不能为空
  • 警告:副本(5006):无法打开流:中不存在文件或目录。。。我的数组从[4]开始而不是从[0]开始??你知道为什么会这样吗

如果有人能帮我实现这个功能,我将不胜感激。我已经在谷歌上搜索了好几天了。

我试过glob(),但没有给我任何价值。。。

提前感谢!:)

我不能解决所有的错误,但我认为第一个错误(未定义的偏移)是因为这行

(false !== $file = readdir($isot))

需要写为

(false !== ($file = readdir($isot)))

因为运算符的优先级。