title: how to rename uploaded files if there's duplicated names
body: <p>i'm having a small issue in the duplicated names.
  I want to auto rename any duplicated upload files, like numbering them.</p>
  
  <p>or if i could make the name same with numbers, such as file1.jpg / file2.jpg
  for all uploaded files</p>
  
  <p>here's my code </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  
   include('connect-db.php');
    
   if (isset($_POST['submit'])) {
    
  
  $filename=  $_FILES["imgfile"]["name"];
    if ((($_FILES["imgfile"]["type"] == "image/gif")|| ($_FILES["imgfile"]["type"] == "image/jpeg") || ($_FILES["imgfile"]["type"] == "image/png")  || ($_FILES["imgfile"]["type"] == "image/pjpeg")) &amp;&amp; ($_FILES["imgfile"]["size"] &lt; 20000000))
  
  
  
    {
      if(file_exists($_FILES["imgfile"]["name"]))
      {
        echo "File name exists.";
      }
  
      else
      {
        move_uploaded_file($_FILES["imgfile"]["tmp_name"],"photos/$filename");
      }
  
  
    }
   
  
    if (is_numeric($_POST['id'])) {
     $id = $_POST['id'];
     
     $id_photo= mysql_real_escape_string(htmlspecialchars($_POST['filename']));
  
     // check that firstname/lastname fields are both filled in
     
     if ($filename== '' ) {
      // generate error message
      $error = 'ERROR: Please fill in all required fields!';
  
  echo("&lt;meta http-equiv='refresh' content='0'&gt;"); //Refresh by HTTP META
  
     } else {
      // save the data to the database
      mysql_query("UPDATE table SET id_photo='$filename' WHERE id='$id' ") or die(mysql_error());
      // once saved, redirect back to the view page
  
  echo("&lt;meta http-equiv='refresh' content='0'&gt;"); //Refresh by HTTP META
  
     }
  
    } else {
     // if the 'id' isn't valid, display an error
     echo 'Error!';
    }
   
   }
   
   
   ?&gt;
   </code></pre>
  </div>
  </div>
  </p>
  
  <p>Even the echo of if(file_exists($_FILES["imgfile"]["name"])) it's not working, i don't know why</p>
  
  <p>Thank you very much before replying</p>