How to upload video using PHP and MySQL Database?

Today, We want to share with you how to upload video in php.In this post we will show you video uploads in php, hear for Upload and Display Video in PHP & Check extension of video before uploading the video file. we will give you demo and example for implement.In this post, we will learn about Upload Video Files Images Using PHP Mysql with an example.

Upload and Store video to MySQL Database with PHP

If video file or format extension is mp4,avi,mov,3gp,mpeg then uploads the reference of video file in MySQL Database Table. Also, show how you can display stored videos in the database.

uploads video in php, upload video in php mysql, how to display video in php from database, multiple video uploads in php, how to insert mp4 video in php, file uploads in php, file uploads in php mysql database, video upload using ajax in php,
uploads video in php

Create Database and Table

step 1:

//create a database tiktok_tools
CREATE DATABASE `tiktok_tools`;
USE tiktok_tools;

//create a table textarea
CREATE TABLE `video`
 (
`v_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`video_name` varchar(255) not null 
)

step 2: PHP Script

<?php

error_reporting(1);

$con=mysql_connect("localhost","root","");

mysql_select_db("tiktok_tools",$con);

extract($_POST);

$target_dir = "test_uploads/";

$target_file = $target_dir . basename($_FILES["profileDpAccount"]["name"]);

if($upd)
{
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if($imageFileType != "mp4" && $imageFileType != "avi" && $imageFileType != "mov" && $imageFileType != "3gp" && $imageFileType != "mpeg")
{
    echo "File Format Not Suppoted";
} 

else
{

$video_path=$_FILES['profileDpAccount']['name'];

mysql_query("insert into video(video_name) values('$video_path')");

move_uploaded_file($_FILES["profileDpAccount"]["tmp_name"],$target_file);

echo "uploaded ";

}

}

//display all uploaded video

if($disp)

{

$query=mysql_query("select * from video");

	while($all_video=mysql_fetch_array($query))

{
?>
	 
	 <video width="300" height="200" controls>
	<source src="test_uploads/<?php echo $all_video['video_name']; ?>" type="video/mp4">
	</video> 
	
<?php } } ?>

HTML Form for Video uploading

step 3:

<form method="post" enctype="multipart/form-data">

<table border="1" style="padding:10px">

<tr>

<Td>Upload  Video</td></tr>

<Tr><td><input type="file" name="profileDpAccount"/></td></tr>

<tr><td>

<input type="submit" value="Uplaod Video" name="upd"/>

<input type="submit" value="Display Video" name="disp"/>

</td></tr>

</table>

</form>

Example 2: Upload and Store video to MySQL Database with PHP

Table structure

Step 1: videos table in the example.

CREATE TABLE `videos` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `vdo_address` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Configuration

Step 2: Create a config.php file

<?php
session_start();
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
  die("Connection failed: " . mysqli_connect_error());
}

Upload and Store Video

step 3:

<?php
include("config.php");
 
if(isset($_POST['but_uploads'])){
   $maxsize = 5242880; // 5MB
   if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != ''){
       $name = $_FILES['file']['name'];
       $target_dir = "videos/";
       $target_file = $target_dir . $_FILES["file"]["name"];

       // Select file type
       $extension = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

       // Valid file extensions
       $extensions_arr = array("mp4","avi","3gp","mov","mpeg");

       // Check extension
       if( in_array($extension,$extensions_arr) ){
 
          // Check file size
          if(($_FILES['file']['size'] >= $maxsize) || ($_FILES["file"]["size"] == 0)) {
             $_SESSION['message'] = "File too large. File must be less than 5MB.";
          }else{
             // Upload
             if(move_uploaded_file($_FILES['file']['tmp_name'],$target_file)){
               // Insert record
               $query = "INSERT INTO videos(name,vdo_address) VALUES('".$name."','".$target_file."')";

               mysqli_query($con,$query);
               $_SESSION['message'] = "Upload successfully.";
             }
          }

       }else{
          $_SESSION['message'] = "Invalid file extension.";
       }
   }else{
       $_SESSION['message'] = "Please select a file.";
   }
   header('location: index.php');
   exit;
} 
?>
<!doctype html> 
<html> 
  <head>
     <title>Upload and Store video to MySQL Database with PHP</title>
  </head>
  <body>

    <!-- Upload response -->
    <?php 
    if(isset($_SESSION['message'])){
       echo $_SESSION['message'];
       unset($_SESSION['message']);
    }
    ?>
    <form method="post" action="" enctype='multipart/form-data'>
      <input type='file' name='file' />
      <input type='submit' value='Upload' name='but_uploads'>
    </form>

  </body>
</html>

Read Videos

step 4: Create readfile.php file

<?php
include("config.php");
?>
<!doctype html>
<html>
  <head>
    <title>Upload and Store video to MySQL Database with PHP - www.pakainfo.com</title>
  </head>
  <body>
    <div>
 
     <?php
     $fetchVideos = mysqli_query($con, "SELECT * FROM videos ORDER BY id DESC");
     while($row = mysqli_fetch_assoc($fetchVideos)){
       $vdo_address = $row['vdo_address'];
       $name = $row['name'];
       echo "<div style='float: left; margin-right: 5px;'>
          <video src='".$vdo_address."' controls width='320px' height='320px' ></video>     
          <br>
          <span>".$name."</span>
       </div>";
     }
     ?>
 
    </div>

  </body>
</html>

I hope you get an idea about uploads video in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.