title: Need A Form Process To Automatically Re-Name Uploaded Photo
body: <p>ISSUE:</p>
  
  <p>I have a php buffering code that 'names' my form-generated .html pages, based on the data in one of the form fields in my form.</p>
  
  
  
  <pre><code>&lt;!--Start Buffer-&gt;
  &lt;?php ob_start(); $URL=preg_replace('#[^_0-9a-zA-Z]#', '_', $_REQUEST['timeValue']); ?&gt; 
  </code></pre>
  
  <p>
      </p>
  
  <p>That particular form field is auto-populated with the current unix millisecond clock.</p>
  
  <pre><code>    &lt;!--FormPage--&gt;
      &lt;!--Timevalue/Unix millisecond code/field-&gt;
  
      &lt;div class="formElement"&gt;TIMESTAMP URL&lt;br&gt;&lt;input id="timeValue" name="timeValue"&gt;&lt;/div&gt;
      &lt;script src="timeValueURL.js"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>Therefore... my .html files are ultimately saved with a 'name' (ie. number), which is the same as whatever the 'milliseconds' were at the moment my form was 'submitted'.</p>
  
  <p>*Note: My 'submit' button also 'updates' the millisecond clock on submit.</p>
  
  <p>The generated .html files are then saved to one of the valid directories; 'categories/news', 'categories/sports' or 'categories/entertainment'.</p>
  
  <p>So, (Example) If the .html file is saved to the 'news' directory... and the millisecond clock was at '1500000000000' when the form was submitted... the .html file would have a url 'mywebsite.com/categories/news/1500000000000.html'</p>
  
  <hr>
  
  <p>In the same form... I have a file upload field for my visitors to upload an image file from their own server/device.</p>
  
  <p>Note: The permitted image file types are limited to JPEG, PNG and GIF.</p>
  
  <p>Those uploaded images are saved in a folder named 'categories-images'.</p>
  
  <p>Currently... those image files are saved to that folder under the 'name' they had when they were uploaded.</p>
  
  <p>Example: If a user uploads a JPEG image named 'blueimage.jpg', the file would currently be saved at the url 'mywebsite.com/categories/category-images/blueimage.jpg'.</p>
  
  <p>Instead... I would like the image file to share the same 'name' as the .html file.</p>
  
  <p>So, using the above example... the image should have a name of '1500000000000.jpg' and have a url address of </p>
  
  <p>'mywebsite.com/categories/category-images/1500000000000.jpg'.</p>
  
  <hr>
  
  <p>QUESTION:</p>
  
  <p>Is there a way to achieve this similar to, or perhaps even incorporating the code used in naming the .html file?</p>
  
  <hr>
  
  <p>NOTE:</p>
  
  <p>It occurred to me that I should include the php for image handling as well.</p>
  
  <p>So here that is....</p>
  
  <pre><code>&lt;?php 
      $target_dir = "articles/article-images/";
      $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1;
      $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
      if(isset($_POST["submit"]))
      {
          $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
          if($check !== false)
          {
              echo "" . $check[""] . ""; $uploadOk = 1;
          } 
          else
          {
              echo "  &amp;#xd7; FILE IS NOT AN IMAGE"; $uploadOk = 0;
          } 
      }
      if(file_exists($target_file))
      {
          echo "  &amp;#xd7; THIS IMAGE ALREADY EXIST ON SERVER"; $uploadOk = 0;
      }
      if ($_FILES["fileToUpload"]["size"] &gt; 500000)
      {
          echo "  &amp;#xd7; FILE IS TOO LARGE"; $uploadOk = 0;
      }
      if($imageFileType != "jpg" &amp;&amp; $imageFileType != "png" &amp;&amp; $imageFileType != "jpeg" &amp;&amp; $imageFileType != "gif" )
      {
          echo "   &amp;#xd7; ONLY JPG, JPEG, PNG &amp; GIF FILES ARE PERMITTED"; $uploadOk = 0; 
      }
      if ($uploadOk == 0)
      {
          echo "  &amp;#xd7; IMAGE WAS NOT UPLOADED";
      }
      else
      {
          if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
          {
              echo '&lt;img class="fixed-ratio-resize" src="../../articles/article-images/'. basename( $_FILES["fileToUpload"]["name"]). '"&gt;';
          } 
          else
          {
              echo "  &amp;#xd7; IMAGE WAS NOT UPLOADED";
          }
      } 
  ?&gt;
  </code></pre>