title: PHP Overwrite Uploaded Image
body: <p>I am trying some  file upload code which I Googled.</p>
  
  <p>Now Issue is when I try to upload image and if its already in folder then its over write and issues come. I Try Code From <a href="https://stackoverflow.com/questions/18972518/rename-a-file-if-already-exists-php-upload-system">here</a> and <a href="https://stackoverflow.com/questions/16136519/php-rename-file-name-if-exists-append-number-to-end">here as well</a> but I face some error.</p>
  
  <p>Here is my code. Can I do in this code that its upload file with some extra name which stop over write of existing file??</p>
  
  <pre><code>if(isset($_REQUEST['main']))
  {
  
      $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
  
        /*if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) &amp;&amp; ($_FILES["file"]["size"] &lt; 20000) &amp;&amp; in_array($extension, $allowedExts))  {*/
  
          if ($_FILES["file"]["error"] &gt; 0) {
              echo "Return Code: " . $_FILES["file"]["error"] . "&lt;br&gt;";
          } else {
  
              if (file_exists("upload/" . $_FILES["file"]["name"])) {
                  echo $_FILES["file"]["name"] . " already exists. ";
              } else {
                  if( move_uploaded_file($_FILES["file"]["tmp_name"], "../img/catalog/" . $_FILES["file"]["name"]) ){
                      $filepath = "img/catalog/" . $_FILES["file"]["name"];
                  }else{
                      echo $_FILES["file"]["name"]." unable to store";
                  }
              }
          /*}
        } else {
          echo "Invalid file";
        }*/
  }
      $main = $_REQUEST['main'];
      $sql="INSERT INTO image VALUES ('', '$filepath', '$main')";
  
      if (!mysqli_query($con,$sql)) {
        die('Error: ' . mysqli_error($con));
      }
      echo "1 record added";
  }
  </code></pre>