title: PHP validate upload and convert files based on file type
body: <p>I have to following code to upload files with php:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Upload your files&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form enctype="multipart/form-data" action="upload.php" method="POST"&gt;
      &lt;p&gt;Upload your file&lt;/p&gt;
      &lt;input type="file" name="uploaded_file"&gt;&lt;/input&gt;&lt;br /&gt;
      &lt;input type="submit" value="Upload"&gt;&lt;/input&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;?PHP
    if(!empty($_FILES['uploaded_file']))
    {
      $path = "uploads/";
      $path = $path . basename( $_FILES['uploaded_file']['name']);
  
      if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
        echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
        " has been uploaded";
      } else{
          echo "There was an error uploading the file, please try again!";
      }
    }
  ?&gt;
  </code></pre>
  
  <p>Its working, but fine my question is how to add/implement a <strong>secure validation</strong> based on file type and also convert files?</p>
  
  <p>All images files should be converted to <strong>jpg type</strong> and all videos files should be converted to <strong>mp4 type</strong>.</p>
  
  <p>In the upload process I also should add <code>randon()</code> name to the file. </p>
  
  <p><strong>IMPORTANT:</strong> after validation, all images should go to <code>upload/images/</code> and all videos to <code>upload/videos/</code></p>