title: PHP How to check if a name already exists on a form post, and if it does adds a number to the name
body: <p>I'm writing a piece of code that uploads a .zip file, takes the content of the .zip file and removes the .zip itself. It also changes the name of the directory based on your own input, which is required. This is the form:</p>
  
  <pre><code>&lt;form action ="" method="post" enctype="multipart/form-data"&gt;
      &lt;div&gt;
          &lt;p&gt;&lt;input type="text" name="NewName" placeholder="Enter New Name" required/&gt;&lt;/p&gt;
          &lt;p&gt;&lt;input type="file" name="zip" required/&gt;&lt;/p&gt;
          &lt;p&gt;&lt;input type="submit" value="upload" /&gt;&lt;/p&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This is the php i use to rewrite the directory, which is, by default, named "NewMap"</p>
  
  <pre><code> if($_POST['NewName']){
        $NewNameStr = $_POST['NewName'];
        $NewNameTrim=preg_replace('/\s+/', '', $NewNameStr);
        rename("uploads/NewMap/", "uploads/" . $NewNameTrim);
        mkdir("uploads/NewMap/");
   }
  </code></pre>
  
  <p>As you can see after I rename it I create the directory again for future use.</p>
  
  <p>My question is the following: It is possible that the user will use a name that already exists, when this happens it does not rename anything and the map structure gets messed up. How can i check if the name already exists with $_POST['NewName']; and add a number to it? So if user1 calls his directory onions and user2 also calls his directory onions user2's directory gets called onions1</p>