How To Make A Video Upload And Plkayback With A Database
Past storing media files in the MySQL database make information technology easier to remember files uploaded past the user or in a specific category.
For this require storing the file on the server and save the reference to the database.
In the tutorial, I show how y'all can upload and store video to the MySQL database table with PHP. Also, show how yous can display stored videos in the database.
Contents
- Table structure
- Configuration
- Upload and Shop Video
- Read Videos
- Conclusion
ane. Table structure
I am using videos
table in the example.
CREATE TABLE `videos` ( `id` int(11) Non Aught Chief KEY AUTO_INCREMENT, `name` varchar(255) NOT Zippo, `location` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
two. Configuration
Create a config.php
file for the database configuration.
Completed Code
<?php session_start(); $host = "localhost"; /* Host name */ $user = "root"; /* User */ $password = ""; /* Password */ $dbname = "tutorial"; /* Database proper name */ $con = mysqli_connect($host, $user, $password,$dbname); // Check connection if (!$con) { die("Connection failed: " . mysqli_connect_error()); }
3. Upload and Store Video
Create a new folder videos
at the project root.
HTML
Using $_SESSION['bulletin']
to display the file upload response. Create a file element and a submit push button in the <form>
.
PHP
On <form >
submit assign max file size 5MB in bytes to $maxsize. You can update this value according to your requirement.
Assign file upload folder to $target_dir
. Read the file extension and assign it to $extension
variable.
Initialize $extensions_arr
Assortment with valid video file extensions.
If file extension exists in $extensions_arr
Array and then check its file size. If it is not exceeded and so upload the file and insert a new record in the videos
table.
Pass $name
in name field and $target_file
in location field.
Completed Lawmaking
<?php include("config.php"); if(isset($_POST['but_upload'])){ $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 = assortment("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 also large. File must exist less than 5MB."; }else{ // Upload if(move_uploaded_file($_FILES['file']['tmp_name'],$target_file)){ // Insert tape $query = "INSERT INTO videos(name,location) VALUES('".$name."','".$target_file."')"; mysqli_query($con,$query); $_SESSION['bulletin'] = "Upload successfully."; } } }else{ $_SESSION['message'] = "Invalid file extension."; } }else{ $_SESSION['message'] = "Please select a file."; } header('location: index.php'); exit; } ?> <!doctype html> <html> <head> <championship>Upload and Store video to MySQL Database with PHP</title> </head> <torso> <!-- Upload response --> <?php if(isset($_SESSION['message'])){ echo $_SESSION['bulletin']; unset($_SESSION['message']); } ?> <form method="mail service" action="" enctype='multipart/form-data'> <input type='file' name='file' /> <input type='submit' value='Upload' proper name='but_upload'> </form> </body> </html>
4. Read Videos
Create readfile.php
file to brandish the stored videos in the videos
tabular array.
Fetch all records from videos
table.
Create <video>
chemical element where pass $location
in the src
aspect and use <span >
to display the name.
Completed Code
<?php include("config.php"); ?> <!doctype html> <html> <head> <title>Upload and Store video to MySQL Database with PHP</title> </head> <body> <div> <?php $fetchVideos = mysqli_query($con, "SELECT * FROM videos Guild By id DESC"); while($row = mysqli_fetch_assoc($fetchVideos)){ $location = $row['location']; $proper noun = $row['name']; repeat "<div fashion='float: left; margin-right: 5px;'> <video src='".$location."' controls width='320px' acme='320px' ></video> <br> <bridge>".$proper name."</span> </div>"; } ?> </div> </body> </html>
v. Determination
Store video file in a directory and insert file location in a MySQL database tabular array. Display file using <video>
element.
Before setting max file size for file size validation make certain to check post_max_size
and upload_max_filesize
in php.ini
file and update it accordingly if required.
If y'all institute this tutorial helpful then don't forget to share.
Are yous desire to get implementation help, or modify or extend the functionality of this script? Submit paid service asking.
Related posts:
Source: https://makitweb.com/upload-and-store-video-to-mysql-database-with-php/
Posted by: graysaight.blogspot.com
0 Response to "How To Make A Video Upload And Plkayback With A Database"
Post a Comment