title: Warning: Invalid argument supplied for foreach() - uploading photos form
body: <p>Hi I'm new to php and keep getting this error: "<strong>Warning: Invalid argument supplied for foreach() in /home/site/folder/upload.php on line 61.</strong>"</p>
  
  <p>I'm trying to build a form in which users can upload one or more photos automatically to a directory to then be displayed else where.</p>
  
  <p>Whenever I use this form I created it functions properly on my website but unfortunately it keeps printing that error out and would like it to go away. Here is my code I'm working with:</p>
  
  <pre><code> &lt;div&gt;
          &lt;form action="upload.php" enctype="multipart/form-data" method="POST"&gt;
              &lt;input type="file" name="images[]" multiple="multiple"/&gt;
              &lt;input type="submit" name="submit" value="upload images"/&gt;
          &lt;form/&gt;
          &lt;?php
                      // check if uploads directory exists
              $dir = "images/";
              if(!is_dir($dir))
              {
                              echo "Directory not found, let's create the folder.";
                              mkdir($dir,"0777", true);
              }
  
              $countimg = 0;
              $allimg = 0;
              foreach($_FILES["images"]["name"] as $k=&gt;$name)
              {
                           $allimg++;
                           $imgname = $_FILES["images"]["name"][$k];
                           $sizeimg = $_FILES["images"]["size"][$k];
                           $tmpname = $_FILES["images"]["tmp_name"][$k];
                                   //2.
                           $extension = strtolower(pathinfo($dir.$imgname, PATHINFO_EXTENSION));
                      if($extension=='png' || $extension=='jpg' ||$extension=='jpeg' ||$extension=='gif')
                                   {
                                          if($sizeimg &lt; 2097152){
                                                          if(!file_exists($dir.$imgname)){
                                                                              //1.
                                                                      if(move_uploaded_file($tmpname,$dir.$imgname))
                                                                      {
                                                                                           $countimg++;
                                                                      }
                                                       }
                                          }
                           }
              }
              echo "You are trying to upload $allimg images".'&lt;br&gt;';
              echo "From $allimg image(s) - $countimg was/were uploaded with success".'&lt;br&gt;';
              $z = $allimg - $countimg;
              echo "$z image(s) were not uploaded: Not an image, over 2MB, or already uploaded.";
  
          ?&gt;
      &lt;/div&gt;
  </code></pre>