Video file is not displaying after converted to .mp4

124 Views Asked by At

I'm trying to create PHP video upload script which convert MOV and many other video files extensions to .mp4 extension. I successfully upload the file to the folder and create a record to the database. But when I try to display that video file in HTML5 video tag is not working.

Here is my PHP code:

  <?php include "includes/dbh.inc.php"; ?>

 <?php
if (isset($_SESSION['user_name'])){
    
    if (isset($_POST['submit'])) {
        $author = $_SESSION['user_name'];
        $post_photo = time() . '-' . $_FILES["post_photo"]["name"];
        if (file_exists($_FILES['post_photo']['tmp_name']) && is_uploaded_file($_FILES['post_photo']['tmp_name'])) {

            $targetvid     = md5(time());
            $target_dirvid = "post_video/";

            $target_filevid = $targetvid . basename($post_photo);

            $uploadOk = 0;

            $videotype = pathinfo($target_filevid, PATHINFO_EXTENSION);

    //these are the valid video formats that can be uploaded and
                  //they will all be converted to .mp4

            $video_formats = [
                "mpeg",
                "mp4",
                "mov",
                "wav",
                "avi",
                "dat",
                "flv",
                "MOV",
                "3gp"
            ];

            foreach ($video_formats as $valid_video_format) {

      //You can use in_array and it is better

                if (preg_match("/$videotype/", $valid_video_format)) {
                    $target_filevid = $targetvid . basename($_FILES["post_photo"] . ".mp4");
                    $uploadOk       = 1;
                    break;

                } 

            }

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {

                echo $message;

                // if everything is ok, try to upload file

            } 

                if (move_uploaded_file($_FILES["post_photo"]["tmp_name"], $target_dirvid . $target_filevid)) {
                    $sql = "INSERT INTO video_posts (author, video_post) VALUES ('$author', '$target_filevid')" ;
                    $result = mysqli_query($conn, $sql);
                    
                   if ($result) {
                    echo "Success";
                   } else {
                    echo "Failed" . mysqli_error($conn);
                   }
                }
            }
        

    }
}
?>

And this is my HTML video tag P.S. I'm using foreach loop to display video for every user.


<video id="myVideo" controls  playsinline>
  <source src="../post_video/<?php echo $user_post['video_post']; ?>" type="video/mp4">
  <source src="../post_video/<?php echo $user_post['video_post']; ?>" type="video/ogg">
</video>

On database record the file name is absolutely the same as in the upload folder with MP4 extension but is still not showing into HTML page.

1

There are 1 best solutions below

0
On

If you mean that your video when it been uploaded does not display in you website change the following:

$target_dirvid = "post_video/";

To:

$target_dirvid = "post_video\";

If you mean that you want to change the extension name to another and you want it to be displayed; you can not do this.