title: PHP Rename File name if Exists Append Number to End
body: <p>I'm trying to rename the file name of an image when it's uploaded if it exists, say if my file name is <code>test.jpg</code> and it already exists I want to rename it as <code>test1.jpg</code> and then <code>test2.jpg</code> and so on. With the code I've written its changing my file name like so <code>test1.jpg</code> and then <code>test12.jpg</code> any advice on fixing this would be great thank!</p>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>$name = $_FILES['picture']['name'];
  $actual_name = pathinfo($name,PATHINFO_FILENAME);
  $extension = pathinfo($name, PATHINFO_EXTENSION);
  
  $i = 1;
  while(file_exists('tmp/'.$actual_name.".".$extension))
  {           
      $actual_name = (string)$actual_name.$i;
      $name = $actual_name.".".$extension;
      $i++;
  }
  </code></pre>