title: PHP check if filename is in use and then add number on upload
body: <p>I'm trying to make a "send email" form which uploads files to a folder before the email is sent. The path to the files are then shown in the email, but if the files DO exist i don't want them to be overwritten, but i want to have a number added.</p>
  
  <p>I have composed this tiny code, but it doesn't work!</p>
  
  <p><strike>
  The error says:</p>
  
  <blockquote>
    <p><strong>Parse error: syntax error, unexpected '$name_of_uploaded_file' (T_VARIABLE) in /customers/8/5/6/HOMEPAGE/httpd.www/php/mailtest2.php on line 49</strong>
    </strike></p>
  </blockquote>
  
  <p>The script below now upload a file if the file does not exist. If the file does exist nothing happens. The script ends at it should.</p>
  
  <p>The script have been modified to what caused it not to ad "_1" to filename and save file in folder. Now it saves the file in folder and comes up with error: <strong>Error while copying the uploaded file</strong></p>
  
  <p>Can someone please explain to me what i do wrong?</p>
  
  <p>Now all code is shown to debug this:</p>
  
  <pre><code>&lt;?php
  //Uploaded file 1
  
  //Settings
  $max_allowed_file_size = 2000; // size in KB
  $allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "pdf");
  error_reporting(E_ALL);
  
  //Upload folder
  //name also used later on mail
  $name = $_POST['name'];
  $d = date('d.m.y');
  $varfoldername = "../receivedfiles/$name$d/";
  if(!is_dir($varfoldername)) {
  mkdir($varfoldername , 0777 , true);
  }
  $upload_folder = $varfoldername;
  
  //Get the uploaded file information
  $name_of_uploaded_file =
      basename($_FILES['uploaded_file']['name']);
  
  
  //Validations
  //----- Check if files exists and adds _X to it -----
  $original_name_of_file = pathinfo($name_of_uploaded_file, PATHINFO_FILENAME);
  $extension =  pathinfo($name_of_uploaded_file, PATHINFO_EXTENSION);
  $FileCounter = 0;
  
  while (file_exists($varfoldername.$name_of_uploaded_file)) {
       $FileCounter++;
       $name_of_uploaded_file = $original_name_of_file . '_' . $FileCounter . '.' . $extension;
  }
  
  //copy the temp. uploaded file to uploads folder
  $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
  $tmp_path = $_FILES["uploaded_file"]["tmp_name"];
  
  if(is_uploaded_file($tmp_path))
  {
    if(move_uploaded_file ( $tmp_path,$path_of_uploaded_file ))
    {
      die("Error while copying the uploaded file");
    }
  }
  
  //Validate size requirements
  $size_of_uploaded_file =
      $_FILES["uploaded_file"]["size"]/1024;//size in KBs
  
  if($size_of_uploaded_file &gt; $max_allowed_file_size )
  {
    die("Fejl: Filen er for stor");
  }
  
  //------ Validate the file extension -----
  //get the file extension of the file
  $type_of_uploaded_file =
      substr($name_of_uploaded_file,
      strrpos($name_of_uploaded_file, '.') + 1);
  $allowed_ext = false;
  for($i=0; $i&lt;sizeof($allowed_extensions); $i++)
  {
    if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
    {
      $allowed_ext = true;
    }
  }
  
  if(!$allowed_ext)
  {
    die("The uploaded file is not supported file type. \n Send venligst filer af følgende type: .implode(',',$allowed_extensions)");
  }
  
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $call = $_POST['call'];
  $company = $_POST['company'];
  $type = $_POST['type'];
  $adress = $_POST['adress'];
  $hesteid = $_POST['hesteid'];
  $hestenavn = $_POST['hestenavn'];
  $message = $_POST['message'];
  $areacode = $_POST['areacode'];
  $land = $_POST['land'];
  $formcontent=" Fra: $company \n Navn: $name \n Adresse: $adress , $areacode \n Land: $land \n Telefon: $phone \n Ringes op: $call \n Type: $type \n Hoppens navn og ID: $hestenavn , $hesteid \n Besked: \n $message \n Vedhæftede filer: \n $path_of_uploaded_file";
  $recipient = "simon@secret.dk";
  $subject = "Besked fra hjemmesiden";
  $mailheader = "Fra: $email \r\n";
  mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
  header('Location: thank_you.shtml');
  ?&gt;
  </code></pre>