title: Rename files in file_upload.php
body: <p>Like an idiot I agreed to create a form for a client (I typically just do HTML and CSS coding) that incorporates PHP for a sendmail/file upload.  I didn't think it would be too difficult with the plethora of tutorials online and I wanted to begin learning PHP anyways.  I was upfront with the client that I have no experience with PHP and gave a significant discount as I look at this as a learning opportunity more than contract work.</p>
  
  <p>I have the form functioning as 2 separate parts during development: </p>
  
  <ol>
  <li>the sendmail.php</li>
  <li>the file_upload.php. </li>
  </ol>
  
  <p>So I'm quite proud of myself. However, I am now trying to rename uploaded files as follows: </p>
  
  <pre><code>ClientCompanyName_ProjectName_FileName_Increment.Extension
  </code></pre>
  
  <p>Yes, this seems long but client wants VERY SIMPLE way of differentiating individual files in a single directory (i don't know how to create new unique directories when client's clients upload new files). </p>
  
  <p>As I said, I had the <code>file_upload.php</code> function working prior to trying to rename the files.  Once I added code that I THOUGHT would rename the file by pulling from <code>FORM</code> input fields, I broke the PHP and am now lost as to how to get it to work as needed.</p>
  
  <p>A simple form is as follows, and the php I have for form_input.php is below.</p>
  
  <pre><code>&lt;form action="./upload.php" method="post" enctype="multipart/form-data"&gt;
  &lt;label for="company"&gt;Company Name:&lt;/label&gt;&lt;input type="text" name="company" id="company" /&gt;
  &lt;br /&gt;
  &lt;label for="proj_name"&gt;Project Name:&lt;/label&gt;&lt;input type="text" name="proj_name" id="proj_name" /&gt;
  &lt;br /&gt;
  &lt;label for="file"&gt;Select a File:&lt;/label&gt;&lt;input type="file" name="userfile" id="file" /&gt;
  &lt;br /&gt;
  &lt;input type="submit" name="submit" id="submit" value="Upload File" /&gt;
  &lt;/form&gt;
  
  &lt;?php
  if (isset($_POST['submit'])) {
      // Configuration - Script Options
      $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension
      $file_basename = substr($filename, 0, strripos($filename, '.')); // Get file name minus extension
      $file_ext = substr($filename, strripos($filename, '.')); // Get file extension
      $filesize = $_FILES['file']['size']; // Get file size
      $allowed_file_types =      
          array('.jpg','.jpeg','.gif','.bmp','.png','.pdf','.doc','.docx','.psd'); 
          // These will be the types of files that are allowed to 
          // pass the upload validation
      $file_counter = 1; // used to increment filename if name already exists 
      $company = $_REQUEST['company']; 
      $project = $_REQUEST['proj_name'];
  
      // File renaming and upload functionality
      if (in_array($file_ext,$allowed_file_types) &amp;&amp; ($filesize &lt; 10000001)) { 
          // Checks to make sure uploaded file(s) is an allowed file 
          // type AND within the allowable file size (currently 10MB)
          // Rename File
          $newfilename = $company . '_' . $proj_name . '_' . $file_basename; 
          // Rename file as (CompanyName_FileName_DateStamp)
          // Loop until an available file name is found
          while (file_exists( "file_uploads/" . $newfilename ))
              $finalfilename = $newfilename . '_' . $file_counter++ . $file_ext; 
          // This will be the File Name shown in the upload destination directory
          // (currently the "file_uploads" directory)
          if (file_exists("file_uploads/" . $finalfilename)) {
              // file already exists error
              echo "This file already exists. Please rename this file and upload again if necessary."; 
          } else {
              move_uploaded_file($_FILES["file"]["tmp_name"], "file_uploads/" . $finalfilename); 
              echo "File uploaded successfully."; 
          } 
      }    elseif (empty($file_basename)) {
           // file selection error
           echo "Please select a file to upload."; 
      } elseif ($filesize &gt; 10000000) {
          //file size error
          echo "The file you are trying to upload is too large. Files must be no larger than 10MB."; 
      } else {
          // file type error
          echo "The file you attempted to upload is not allowed. You can only upload the following types of files: .jpg, .jpeg, .gif, .bmp, .png, .pdf, .doc, .docx, and .psd."; 
          unlink($_FILES["file"]["tmp_name"]); 
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>When I try to upload a file now I am getting variations of the following error:</p>
  
  <blockquote>
    <p>Parse error: syntax error, unexpected T_ELSE in /home4/yourpass/public_html/upload/upload.php on line 27</p>
  </blockquote>
  
  <p>When I fix one error, a new similar error occurs on a different line, and finally I get an error that states <em>File already exists</em> but there is no duplicate file in the directory on the server.</p>
  
  <p>For those of you who would like to try a live version of this form you can do so at this link:</p>
  
  <p><a href="http://www.niagarathistle.com/upload/form_upload.html" rel="nofollow">http://www.niagarathistle.com/upload/form_upload.html</a></p>
  
  <p>Any help or a nudge in the right direction would be appreciated. Thanks!</p>
  
  <p>PS: Sorry for the code formatting. I'm still trying to figure out how to EASILY get my code in and formatted properly using Markdown.</p>