Home >>MySQL Tutorial >Upload and Display Video in PHP

Upload and Display Video in PHP

How to upload video using PHP and MySQL

How to Upload video using PHP and MySQL. Check extension of video before uploading the video file. If video file extension is mp4,avi,mov,3gp,mpeg then upload the reference of video file in MySQL Table.

Create Database and Table

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

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

PHP Script

<?php

error_reporting(1);

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

mysql_select_db("demo",$con);

extract($_POST);

$target_dir = "test_upload/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["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['fileToUpload']['name'];

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

move_uploaded_file($_FILES["fileToUpload"]["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_upload/<?php echo $all_video['video_name']; ?>" type="video/mp4">
	</video> 
	
	<?php } } ?>

Note : First create a folder manually named "test_upload". Because uploaded video file stored in "test_upload" folder.

HTML Form for Video uploading

<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="fileToUpload"/></td></tr>

<tr><td>

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

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

</td></tr>

</table>

</form>