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

时间:2012-03-01 14:00:55

标签: php arrays image

我有两个名为'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>");

  }
  }

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

  • 警告:copy():文件名不能为空
  • 警告:复制(5006):无法打开流:文件或目录不存在于...... 我的数组从[4]而不是[0]开始?有什么想法会发生这种情况吗?

如果有人可以帮助我让这个功能起作用,我真的很感激。我一直在谷歌搜索几天。

我已经尝试glob(),但这并没有给我任何价值......

提前谢谢! :)

1 个答案:

答案 0 :(得分:0)

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

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

需要写成

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

因为运营商优先。