How to Upload Video Files using PHP MySQL

March 19, 2021
PHP
upload video files uing php and mysql

Tutorial: How to Upload Video Files using PHP MySQL with Source Code

About How to Upload Video Files using PHP MySQL

In this tutorial we will create on How to Upload Video Files using PHP MySQL. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by newly coders for its user-friendly environment.

Getting Started

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that is used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that is used for the layout design https://getbootstrap.com/.

Compile the downloaded libraries in a folder where you will compile the source code files. Also, create a new folder naming “video” for the location of the uploaded videos.

Creating Database

Open your database web server, then create a database name in it db_video. After that, click ” Import, ” then locate the database file inside the application folder and then click ok.

How to Upload Video Files using PHP MySQL with Source Code

How to Upload Video Files using PHP MySQL with Source Code

You can also create the table programmatically by pasting the SQL code below in your newly created database SQL tab.

CREATE TABLE `video` (
  `video_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `video_name` VARCHAR(100) NOT NULL,
  `location` VARCHAR(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

<?php
	$conn = mysqli_connect('localhost', 'root', '', 'db_video');
 
	if(!$conn){
		die("Error: Failed to connect to database!");
	}
?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
		<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
	</head>
<body>
	<nav class="navbar navbar-default">
		<div class="container-fluid">
			<a href="https://campcodes.com" class="navbar-brand">CampCodes</a>
		</div>
	</nav>
	<div class="col-md-3"></div>
	<div class="col-md-6 well">
		<h3 class="text-primary">PHP - Simple Video Upload</h3>
		<hr style="border-top:1px dotted #ccc;"/>
		<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Video</button>
		<br /><br />
		<hr style="border-top:3px solid #ccc;"/>
		<?php
			require 'conn.php';
 
			$query = mysqli_query($conn, "SELECT * FROM `video` ORDER BY `video_id` ASC") or die(mysqli_error());
			while($fetch = mysqli_fetch_array($query)){
		?>
		<div class="col-md-12">
			<div class="col-md-4" style="word-wrap:break-word;">
				<br />
				<h4>Video Name</h4>
				<h5 class="text-primary"><?php echo $fetch['video_name']?></h5>
			</div>
			<div class="col-md-8">
				<video width="100%" height="240" controls>
					<source src="<?php echo $fetch['location']?>">
				</video>
			</div>
			<br style="clear:both;"/>
			<hr style="border-top:1px groovy #000;"/>
		</div>
		<?php
			}
		?>
	</div>
	<div class="modal fade" id="form_modal" aria-hidden="true">
		<div class="modal-dialog">
			<form action="save_video.php" method="POST" enctype="multipart/form-data">
				<div class="modal-content">
					<div class="modal-body">
						<div class="col-md-3"></div>
						<div class="col-md-6">
							<div class="form-group">
								<label>Video File</label>
								<input type="file" name="video" class="form-control-file"/>
							</div>
						</div>
					</div>
					<div style="clear:both;"></div>
					<div class="modal-footer">
						<button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
						<button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
					</div>
				</div>
			</form>
		</div>
	</div>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>

Creating the Main Function

This code contains the main function of the application. This code will store the video details to the database server, and transfer the video file inside the directory To do this just copy and write the code below inside the text editor, then save it as save_video.php

<?php
	date_default_timezone_set('Asia/Manila');
	require_once 'conn.php';
 
	if(ISSET($_POST['save'])){
		$file_name = $_FILES['video']['name'];
		$file_temp = $_FILES['video']['tmp_name'];
		$file_size = $_FILES['video']['size'];
 
		if($file_size < 50000000){
			$file = explode('.', $file_name);
			$end = end($file);
			$allowed_ext = array('avi', 'flv', 'wmv', 'mov', 'mp4');
			if(in_array($end, $allowed_ext)){
				$name = date("Ymd").time();
				$location = 'video/'.$name.".".$end;
				if(move_uploaded_file($file_temp, $location)){
					mysqli_query($conn, "INSERT INTO `video` VALUES('', '$name', '$location')") or die(mysqli_error());
					echo "<script>alert('Video Uploaded')</script>";
					echo "<script>window.location = 'index.php'</script>";
				}
			}else{
				echo "<script>alert('Wrong video format')</script>";
				echo "<script>window.location = 'index.php'</script>";
			}
		}else{
			echo "<script>alert('File too large to upload')</script>";
			echo "<script>window.location = 'index.php'</script>";
		}
	}
?>

how to upload video files using php mysql

DEMO

There you have it we successfully created Simple Video Upload Using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.


Related Tutorials: Multiple File Upload using AngularJS in PHP/MySQLi, Image Upload using Ajax in PHP, Upload JSON Object To PDO using PHP, Delete Uploaded File in MySQL using PHP, Form File Upload Using Ajax in PHP, How to Upload and Validate an Image File in PHP

Download Here
Comments
  • Warning: mysqli_connect(): (HY000/1045): ProxySQL Error: Access denied for user ‘root’@’2a02:4780:bad:f00d::16’ (using password: NO) in /storage/ssd5/056/19528056/public_html/conn.php on line 2
    Error: Failed to connect to database!

    souzx17 September 6, 2022 10:42 pm Reply
  • im getting this error.

    Warning: move_uploaded_file(video/202201131642058507.mp4): Failed to open stream: No such file or directory in C:\xampp\htdocs\video\save_video.php on line 17

    Warning: move_uploaded_file(): Unable to move “C:\xampp\tmp\php5686.tmp” to “video/202201131642058507.mp4” in C:\xampp\htdocs\video\save_video.php on line 17

    Boombot January 13, 2022 3:25 pm Reply
  • Fatal error: Uncaught mysqli_sql_exception: Unknown database ‘db_video’ in C:\xampp\htdocs\video\conn.php:2 Stack trace: #0 C:\xampp\htdocs\video\conn.php(2): mysqli_connect(‘localhost’, ‘root’, ”, ‘db_video’) #1 C:\xampp\htdocs\video\index.php(21): require(‘C:\\xampp\\htdocs…’) #2 {main} thrown in C:\xampp\htdocs\video\conn.php on line 2

    how to fix this error ??

    Anonymous January 5, 2022 12:36 pm Reply
  • please i am getting the below error code

    Warning: move_uploaded_file(video/202105201621490135.mp4): failed to open stream: No such file or directory in C:\xampp\htdocs\video\save_video.php on line 17

    Warning: move_uploaded_file(): Unable to move ‘C:\xampp\tmp\php865B.tmp’ to ‘video/202105201621490135.mp4’ in C:\xampp\htdocs\video\save_video.php on line 17
    

    dont know how to salvage it, will be grateful if you help

    Catalyst May 20, 2021 1:58 pm Reply
    • Please run on php version 7.3.

      admin May 21, 2021 4:53 pm Reply
  • Helloo, i am getting this error

    Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp64\www\dbvideo\videoupload\save_video.php on line 18

    any idea on how i can fix it

    Neli April 20, 2021 7:00 pm Reply
    • Please try xampp with the latest version not wamp.

      admin April 20, 2021 11:34 pm Reply
    • add $conn as parameter to the function

      Jumamki January 21, 2022 3:56 pm Reply

Leave a Reply

Your email address will not be published. Required fields are marked *