title: Replace previous image and add new image to server with PHP
body: <p>I'm currently working on an '<strong>Edit Project</strong>' script that allows users to replace images on my site. This part works great.</p>
  
  <p>However, I also <strong>want the option to allow new images to be uploaded</strong> - these do not replace existing ones.
  So, in summary, the user can replace existing images AND upload new images using the form.</p>
  
  <p><strong>Question:</strong> <em>How do I tell my PHP that some images are <strong>new</strong> and not replacement ones?</em> I need to do this so I can run INSERT INTO queries on the new images.</p>
  
  <p>I've attached a portion of my code below:</p>
  
  <p><strong>PHP to process replacement of existing files:</strong></p>
  
  <pre><code>    // get original name for ProjectImage update
      $query3 = "SELECT * FROM ProjectImage as pi WHERE pid = '$id' ORDER BY pi.id";
      $result3 = mysql_query ($query3); // Run the Query
      $row3 = mysql_fetch_array($result3);
      $originalname = $row3[1];
  
      $number_of_file_fields = 0;
      $number_of_uploaded_files = 0;
      $number_of_moved_files = 0;
      $uploaded_files = array();
      $upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory
  
      for ($i = 0; $i &lt; count($_FILES['images']['name']); $i++) {
          $number_of_file_fields++;
          if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
              $number_of_uploaded_files++;
              $uploaded_files[] = $_FILES['images']['name'][$i];
              if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
                  $number_of_moved_files++;
                  $url = $_FILES['images']['name'][$i];
                  list($width, $height, $type, $attr) = getimagesize("uploaded/".$_FILES["images"]['name'][$i]);
                  $pieces = explode("_", $url);
                  $ImageName = $pieces[1];    
                  mysql_query("UPDATE ProjectImage SET Name = '$ImageName', Url = '$url', UrlHeight =  '$height' WHERE Name = '$originalname' AND Pid = '$id'");
              }    
          }    
      }
  </code></pre>
  
  <p>
  
  <p>&amp;</p>
  
  <p><strong>PHP used to generate list of existing images:</strong></p>
  
  <pre><code>$query2 = "SELECT * FROM ProjectImage as pi WHERE pid = '$id' ORDER BY pi.id";
  $result2 = mysql_query ($query2); // Run the Query
  while ($row2 = mysql_fetch_array($result2))
  {
  ?&gt;
  &lt;div id="file_container" class="control-group"&gt;
      &lt;label for="image" class="control-label"&gt;Image:&lt;/label&gt;
      &lt;div class="controls"&gt;
          &lt;input name="images[]" type="file"  /&gt;
          &lt;img src="uploaded/&lt;?php echo $row2['Url']; ?&gt;" /&gt;
          &lt;a href="delete.php?id=&lt;?php echo $row2['id']; ?&gt;"&gt;Delete&lt;/a&gt;
          &lt;br /&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;?php
  }
  ?&gt;
  &lt;a href="javascript:void(0);" onClick="add_file_field();"&gt;Add another&lt;/a&gt;&lt;br /&gt;
  </code></pre>
  
  <p>Many thanks for any pointers :-)</p>