title: Upload video and image in php
body: <p>i am creating a blog where on admin side i can upload images and videos, and on user side i can access them. now i want to store images on different directory and videos on different directory, but the problem is that how can i create a logic when the file is selected then the code knows that it is image or video? i try...</p>
  
  <pre><code>&lt;?php
  session_start();
  include 'conn.php';
  $title=$_POST['title'];
  $post=$_POST['post'];
  $tag=$_POST['tag'];
  $cat='some cat';
  $file_name=$_FILES['fileToUpload']['name'];
  $file_size=$_FILES['fileToUpload']['size'];
  $file_height=200;
  $file_width=100;
  $duration=24.00;
  $target_dir_image = "../posts/images/";
  $target_dir_video = "../posts/videos/";
  $target_file_image = $target_dir_image . basename($_FILES["fileToUpload"]["name"]);
  $target_file_video = $target_dir_video . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  
  $imageFileType = pathinfo($target_file_image,PATHINFO_EXTENSION);
  $videoFileType = pathinfo($target_file_video,PATHINFO_EXTENSION);
  
      if ($imageFileType == "jpg" &amp;&amp; $imageFileType == "png" &amp;&amp; $imageFileType == "jpeg" &amp;&amp; $imageFileType == "gif") {
          $uploadOk = 0;
      }
  
  // Check if file already exists
          if (file_exists($target_file_image)) {
              echo "Sorry, file already exists.";
              $uploadOk = 0;
          }
  // Check file size
          if ($_FILES["fileToUpload"]["size"] &gt; 50000000) {
              echo "Sorry, your file is too large.";
              $uploadOk = 0;
          }
  // Allow certain file formats
  
  // Check if $uploadOk is set to 0 by an error
          if ($uploadOk == 0) {
              $_SESSION['error'] = "Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed.";
              header('location:new-post.php');
  // if everything is ok, try to upload file
          }
      else {
              if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_image)) {
                  $conn-&gt;beginTransaction();
                  $conn-&gt;exec("INSERT INTO post(Title,Post,Category,Tag,Post_Date)VALUES ('" . $title . "','" . $post . "','" . $cat . "','" . $tag . "',now())");
                  $conn-&gt;exec("INSERT INTO images(Image_Name,Image_Size,Image_Width,Image_height,Image_Directory)VALUES ('" . $file_name . "','" . $file_size . "','" . $file_width . "','" . $file_height . "','" . $target_file_image . "')");
                  $conn-&gt;commit();
                  $_SESSION['success'] = 'Post has been successfuly published';
                  header('location:new-post.php');
                  $conn-&gt;rollBack();
                  $_SESSION['error'] = 'Fail to publish the post';
                  header('location:new-post.php');
  
  
              }
  
      }
  if ($videoFileType == "mp4" &amp;&amp; $imageFileType == "flv" &amp;&amp; $imageFileType == "mpeg" &amp;&amp; $imageFileType == "avi")
  {
      $uploadOk = 0;
  }
  
  // Check if file already exists
          if (file_exists($target_file_video)) {
              echo "Sorry, file already exists.";
              $uploadOk = 0;
          }
  // Check file size
          if ($_FILES["fileToUpload"]["size"] &gt; 50000000) {
              echo "Sorry, your file is too large.";
              $uploadOk = 0;
          }
  // Allow certain file formats
  
  // Check if $uploadOk is set to 0 by an error
          if ($uploadOk == 0) {
              $_SESSION['error'] = "Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed.";
              header('location:new-post.php');
  // if everything is ok, try to upload file
          } else {
              if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_video)) {
                  $conn-&gt;beginTransaction();
                  $conn-&gt;exec("INSERT INTO post(Title,Post,Category,Tag,Post_Date)VALUES ('" . $title . "','" . $post . "','" . $cat . "','" . $tag . "',now())");
                  $conn-&gt;exec("INSERT INTO videos(Video_Name,Video_Size,Video_Duration,Video_Dimension,Video_Directory)VALUES ('" . $file_name . "','" . $file_size . "','".$duration."','" . $file_width . "','" . $target_file_video . "')");
                  $conn-&gt;commit();
                  $_SESSION['success'] = 'Post has been successfuly published';
                  header('location:new-post.php');
                  $conn-&gt;rollBack();
                  $_SESSION['error'] = 'Fail to publish the post';
                  header('location:new-post.php');
  
  
              } else {
                  echo "Sorry, there was an error uploading your file.";
              }
  
          }
  
  ?&gt;
  </code></pre>
  
  <p>but the above code is not working properly. if i upload only image then the code will work fine but when i use if and else statment then the data is inserted only in image table?</p>
  
  <p><strong>update</strong></p>
  
  <p>i try the below code but it will also not working... </p>
  
  <pre><code>&lt;?php
  session_start();
  include 'conn.php';
  $title=$_POST['title'];
  $post=$_POST['post'];
  $tag=$_POST['tag'];
  $cat='some cat';
  $file=$_FILES['fileToUpload'];
  $file_name=$_FILES['fileToUpload']['name'];
  $file_size=$_FILES['fileToUpload']['size'];
  $file_height=200;
  $file_width=100;
  $duration=24.00;
  $target_dir_image = "../posts/images/";
  $target_dir_video = "../posts/videos/";
  $target_file_image = $target_dir_image . basename($_FILES["fileToUpload"]["name"]);
  $target_file_video = $target_dir_video . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $ext= pathinfo($file_name,PATHINFO_EXTENSION);
  
  
      if ($ext == "jpg" &amp;&amp; $ext  == "png" &amp;&amp; $ext  == "jpeg" &amp;&amp; $ext == "gif")
      {
  
  
  // Check if file already exists
      if (file_exists($target_file_image)) {
          echo "Sorry, file already exists.";
          $uploadOk = 0;
      }
  // Check file size
      if ($_FILES["fileToUpload"]["size"] &gt; 50000000) {
          echo "Sorry, your file is too large.";
          $uploadOk = 0;
      }
  // Allow certain file formats
  
  // Check if $uploadOk is set to 0 by an error
      if ($uploadOk == 0) {
          $_SESSION['error'] = "Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed.";
          header('location:new-post.php');
  // if everything is ok, try to upload file
      } else {
          if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_image)) {
              $conn-&gt;beginTransaction();
              $conn-&gt;exec("INSERT INTO post(Title,Post,Category,Tag,Post_Date)VALUES ('" . $title . "','" . $post . "','" . $cat . "','" . $tag . "',now())");
              $conn-&gt;exec("INSERT INTO images(Image_Name,Image_Size,Image_Width,Image_height,Image_Directory)VALUES ('" . $file_name . "','" . $file_size . "','" . $file_width . "','" . $file_height . "','" .$target_file_image. "')");
              $conn-&gt;commit();
              $_SESSION['success'] = 'Post has been successfuly published';
              header('location:new-post.php');
              $conn-&gt;rollBack();
              $_SESSION['error'] = 'Fail to publish the post';
              header('location:new-post.php');
  
  
          }
          else {
              echo "Sorry, there was an error uploading your file.";
          }
  
      }
  }
  
      if ($ext == "mp4" &amp;&amp; $ext == "flv" &amp;&amp; $ext == "mpeg" &amp;&amp; $ext == "avi") {
  
  
  // Check if file already exists
          if (file_exists($target_file_video)) {
              echo "Sorry, file already exists.";
              $uploadOk = 0;
          }
  // Check file size
          if ($_FILES["fileToUpload"]["size"] &gt; 50000000) {
              echo "Sorry, your file is too large.";
              $uploadOk = 0;
          }
  // Allow certain file formats
  
  // Check if $uploadOk is set to 0 by an error
          if ($uploadOk == 0) {
              $_SESSION['error'] = "Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed.";
              header('location:new-post.php');
  // if everything is ok, try to upload file
          }
      }
      else {
          if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_video)) {
              $conn-&gt;beginTransaction();
              $conn-&gt;exec("INSERT INTO post(Title,Post,Category,Tag,Post_Date)VALUES ('" . $title . "','" . $post . "','" . $cat . "','" . $tag . "',now())");
              $conn-&gt;exec("INSERT INTO videos(Video_Name,Video_Size,Video_Duration,Video_Dimension,Video_Directory)VALUES ('" . $file_name . "','" . $file_size . "','" . $duration . "','" . $file_width . "','" .$target_file_video. "')");
              $conn-&gt;commit();
              $_SESSION['success'] = 'Post has been successfuly published';
              header('location:new-post.php');
              $conn-&gt;rollBack();
              $_SESSION['error'] = 'Fail to publish the post';
              header('location:new-post.php');
  
  
          }     else {
              echo "Sorry, there was an error uploading your file.";
          }
  
      }
  
  ?&gt;
  </code></pre>
  
  <p><strong>What i want</strong></p>
  
  <p><em>when a file is selected then the code checks that it is image or video, if the file is image then it will uploaded to images directory and the data related to image  will store in images table and if the file is video then it will uploaded to videos directory and the data related to video will store in video table and also suggest me the way to get the image/video height and width and video duration.</em></p>
  
  <p><strong>Note:</strong> i am using php 5.5 and may be i upgrade to php 5.5x so please provide such solution which is not out of date mean the code support or not deprecated in php 5.5 and above.</p>