title: PHP file upload with preset file name
body: <p>I was wondering if someone could help me figure this out. I want to be able to upload a picture to a users director with PHP, but I want it to change the default name to avatar.whatever. Here is the code I have so far which is just the w3schools version slightly modified:</p>
  
  <pre><code>&lt;?php
  session_start();
  
  $allowedExts = array("gif", "jpeg", "jpg", "png");
  $extension = end(explode(".", $_FILES["avatar"]["name"]));
  if ((($_FILES["avatar"]["type"] == "image/gif")
  || ($_FILES["avatar"]["type"] == "image/jpeg")
  || ($_FILES["avatar"]["type"] == "image/jpg")
  || ($_FILES["avatar"]["type"] == "image/pjpeg")
  || ($_FILES["avatar"]["type"] == "image/x-png")
  || ($_FILES["avatar"]["type"] == "image/png"))
  &amp;&amp; ($_FILES["avatar"]["size"] &lt; 100000)
  &amp;&amp; in_array($extension, $allowedExts))
    {
    if ($_FILES["avatar"]["error"] &gt; 0)
      {
      echo "Return Code: " . $_FILES["avatar"]["error"] . "&lt;br \&gt;";
      }
    else
      {
      echo "Upload: " . $_FILES["avatar"]["name"] . "&lt;br&gt;";
      echo "Type: " . $_FILES["avatar"]["type"] . "&lt;br&gt;";
      echo "Size: " . ($_FILES["avatar"]["size"] / 1024) . " kB&lt;br&gt;";
      echo "Temp file: " . $_FILES["avatar"]["tmp_name"] . "&lt;br&gt;";
  
      $home_dir = "users/" . $_SESSION["user"] . "/";
  
      if (file_exists($home_dir . $_FILES["avatar"]["name"]))
        {
        echo $_FILES["avatar"]["name"] . " already exists. ";
        }
      else
        {
        move_uploaded_file($_FILES["avatar"]["tmp_name"],$home_dir . $_FILES["avatar"]["name"]);
        echo "Stored in: " . "upload/" . $_FILES["avatar"]["name"];
        }
      }
    }
  else
    {
    echo "Invalid file";
    }
  ?&gt;
  </code></pre>
  
  <p>The form section works good and I would get rid of the if statement that checks if a file already exists and not output as much but can someone help me figure out how to set a pre-defined file name. The user would be able to select a picture file and then it will upload to their directory. Thank you</p>