Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

PHP 7 Script to Upload Large Size Videos to MySQL Database and Display & Play it in Browser

Posted on January 23, 2023

 

 

Welcome folks today in this blog post we will be uploading large video file size to mysql database and display it inside the browser. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to start the xammp control panel and start the apache server and also start the mysql database as shown below

 

 

 

 

And now we will be seeing the below directory structure of the php app as shown below. Now we need to create the uploads directory where we will be storing the video files which will be uploaded by the user.

 

 

 

 

Creating Table

 

 

Now inside the phpadmin area we need to create the videos table where we will have two columns such as the id and the video_url.

 

 

 

 

 

Now we need to make the index.php file and copy paste the following code

 

 

index.php

 

 

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>video upload php and mysql</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
min-height: 100vh;
}
input {
font-size: 2rem;
}
a {
text-decoration: none;
color: #006CFF;
font-size: 1.5rem;
}
</style>
</head>
<body>
<a href="view.php">Videos</a>
<?php if (isset($_GET['error'])) {  ?>
<p><?=$_GET['error']?></p>
<?php } ?>
<form action="upload.php"
       method="post"
       enctype="multipart/form-data">
<input type="file"
       name="my_video">
<input type="submit"
       name="submit"
       value="Upload">
</form>
</body>
</html>

 

 

 

 

 

 

As you can see we have the simple input field where we allow the user to select the video file and then we have the button to upload the video file.

 

 

Now we need to create the db_conn.php file where we will be storing the credentials to connect to the mysql database as shown below

 

 

db_conn.php

 

 

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php  
$sname = "localhost";
$uname = "root";
$password = "";
$db_name = "test_db";
$conn = mysqli_connect($sname, $uname, $password, $db_name);
if (!$conn) {
echo "Connection failed!";
exit();
}
else{
echo "connection successful";
}

 

 

Now when the user submits the form we will be calling the upload.php script where we will be writing the code to upload the video file and insert the link inside the mysql table as shown below

 

 

upload.php

 

 

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
if (isset($_POST['submit']) && isset($_FILES['my_video'])) {
include "db_conn.php";
    $video_name = $_FILES['my_video']['name'];
    $tmp_name = $_FILES['my_video']['tmp_name'];
    $error = $_FILES['my_video']['error'];
    if ($error === 0) {
     $video_ex = pathinfo($video_name, PATHINFO_EXTENSION);
     $video_ex_lc = strtolower($video_ex);
     $allowed_exs = array("mp4", 'webm', 'avi', 'flv');
     if (in_array($video_ex_lc, $allowed_exs)) {
    
     $new_video_name = uniqid("video-", true). '.'.$video_ex_lc;
     $video_upload_path = 'uploads/'.$new_video_name;
     move_uploaded_file($tmp_name, $video_upload_path);
     // Now let's Insert the video path into database
            $sql = "INSERT INTO videos(video_url)
                   VALUES('$new_video_name')";
            mysqli_query($conn, $sql);
            header("Location: view.php");
     }else {
     $em = "You can't upload files of this type";
     header("Location: index.php?error=$em");
     }
    }
}else{
header("Location: index.php");
}

 

 

As you can see we are applying some validation filters before we upload the video file. And then after uploading the file we are redirecting the user to index.php file as shown below

 

 

 

 

 

 

Now we need to create the view.php file where we will be displaying and playing the video with advanced controls inside the browser as shown below

 

 

view.php

 

 

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View</title>
<style>
body {
    display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
min-height: 100vh;
}
video {
width: 640px;
height: 360px;
}
a {
text-decoration: none;
color: #006CFF;
font-size: 1.5rem;
}
</style>
</head>
<body>
<a href="index.php">UPLOAD</a>
<div class="alb">
<?php
include "db_conn.php";
$sql = "SELECT * FROM videos ORDER BY id DESC";
$res = mysqli_query($conn, $sql);
if (mysqli_num_rows($res) > 0) {
while ($video = mysqli_fetch_assoc($res)) {
?>
 
        <video src="uploads/<?=$video['video_url']?>"
            controls>
        
        </video>
    <?php
     }
}else {
echo "<h1>Empty</h1>";
}
?>
</div>
</body>
</html>

 

 

As you can see we are fetching the video links from the table and then displaying it inside the browser using controls with the help of the video tag shown below

 

 

 

 

Recent Posts

  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Android Java Project to Detect System Hardware & System CPU Info & Display inside TextView Widget
  • Android Java Project to Integrate Google OAuth2 Login & Logout System & Save User Info in SharedPreferences
  • Android Java Project to Export Raw Text to PDF Document Using iTextPDF Library
  • Android Java Project to Export Images From Gallery to PDF Document Using iTextPDF Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme