-5

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
popov
  • 1
  • 1
  • 4
    You cannot convert a video to another format by simply changing the file extension. You actually have to process the video frame by frame and save it in the new format. See; [FFmpeg](https://www.ffmpeg.org/). – KIKO Software Sep 09 '21 at 21:35
  • What i need to looking for to do it. My goal is to create upload video/image website but same of the formats around like Apple's ones is not easy to handle. Any advice what i need to learn and looking for to complete my task. Thanks. – popov Sep 09 '21 at 21:37
  • 2
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 09 '21 at 21:38
  • 3
    If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo & https://websitebeaver.com/php-pdo-prepared-statements-to-prevent-sql-injection – Dharman Sep 09 '21 at 21:42
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 15 '21 at 17:13

1 Answers1

-2

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.

David Buck
  • 3,752
  • 35
  • 31
  • 35