title: PHP - Upload, rename and replace
body: <p>I have been trying to find a way to upload an image. Well I can upload an image, but I can't get it to do the following.</p>
  
  <p>Firstly I want it to be restricted to only PNG images
  then I want it to be renamed to pic.png
  lastly it will be replacing another file called pic.png in the directory above the uploader script.</p>
  
  <p>I've tried many different scripts all over the internet but couldn't find one that would work. Here is my current code for the uploader.</p>
  
  <pre><code>&lt;?php
      $allowedExts = array("png");
      $extension = end(explode(".", $_FILES["file"]["name"]));
      if ((($_FILES["file"]["type"] == "image/png"))
      &amp;&amp; ($_FILES["file"]["size"] &lt; 2000000)
      &amp;&amp; in_array($extension, $allowedExts)) {
          if ($_FILES["file"]["error"] &gt; 0) {
              echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br /&gt;";
          } else {
              echo "Upload: " . $_FILES["file"]["name"] . "&lt;br /&gt;";
              echo "Type: " . $_FILES["file"]["type"] . "&lt;br /&gt;";
              echo "Size: " . ($_FILES["file"]["size"] / 2000) . " Kb&lt;br /&gt;";
              echo "Temp file: " . $_FILES["file"]["tmp_name"] . "&lt;br /&gt;";\
              if (file_exists("upload/" . $_FILES["file"]["name"])) {
                  echo $_FILES["file"]["name"] . " already exists. ";
              } else {
                  move_uploaded_file($_FILES["file"]["tmp_name"],
                  "../" . $_FILES["file"]["name"]);
                  echo "Stored in: " . "../" . $_FILES["file"]["name"];
              }
          }
      } else {
          echo "Invalid file";
      }
  ?&gt;
  </code></pre>
  
  <p>Thanks heaps internet!</p>