How to Upload Video Files using PHP

In this tutorial we will create a Simple Video Upload using PHP. 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.

So Let's do the coding...

Getting Started

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

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

Lastly, this is the link for the bootstrap that i 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 folder of the application then click ok.

tut1

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

  1. CREATE TABLE `video` (
  2. `video_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  3. `video_name` VARCHAR(100) NOT NULL,
  4. `location` VARCHAR(100) NOT NULL
  5. ) 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.

  1. <?php
  2. $conn = mysqli_connect('localhost', 'root', '', 'db_video');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Video Upload</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Video</button>
  18. <br /><br />
  19. <hr style="border-top:3px solid #ccc;"/>
  20. <?php
  21. require 'conn.php';
  22.  
  23. $query = mysqli_query($conn, "SELECT * FROM `video` ORDER BY `video_id` ASC") or die(mysqli_error());
  24. while($fetch = mysqli_fetch_array($query)){
  25. ?>
  26. <div class="col-md-12">
  27. <div class="col-md-4" style="word-wrap:break-word;">
  28. <br />
  29. <h4>Video Name</h4>
  30. <h5 class="text-primary"><?php echo $fetch['video_name']?></h5>
  31. </div>
  32. <div class="col-md-8">
  33. <video width="100%" height="240" controls>
  34. <source src="<?php echo $fetch['location']?>">
  35. </video>
  36. </div>
  37. <br style="clear:both;"/>
  38. <hr style="border-top:1px groovy #000;"/>
  39. </div>
  40. <?php
  41. }
  42. ?>
  43. </div>
  44. <div class="modal fade" id="form_modal" aria-hidden="true">
  45. <div class="modal-dialog">
  46. <form action="save_video.php" method="POST" enctype="multipart/form-data">
  47. <div class="modal-content">
  48. <div class="modal-body">
  49. <div class="col-md-3"></div>
  50. <div class="col-md-6">
  51. <div class="form-group">
  52. <label>Video File</label>
  53. <input type="file" name="video" class="form-control-file"/>
  54. </div>
  55. </div>
  56. </div>
  57. <div style="clear:both;"></div>
  58. <div class="modal-footer">
  59. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  60. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  61. </div>
  62. </div>
  63. </form>
  64. </div>
  65. </div>
  66. <script src="js/jquery-3.2.1.min.js"></script>
  67. <script src="js/bootstrap.js"></script>
  68. </body>
  69. </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.

  1. <?php
  2. date_default_timezone_set('Asia/Manila');
  3. require_once 'conn.php';
  4.  
  5. if(ISSET($_POST['save'])){
  6. $file_name = $_FILES['video']['name'];
  7. $file_temp = $_FILES['video']['tmp_name'];
  8. $file_size = $_FILES['video']['size'];
  9.  
  10. if($file_size < 50000000){
  11. $file = explode('.', $file_name);
  12. $end = end($file);
  13. $allowed_ext = array('avi', 'flv', 'wmv', 'mov', 'mp4');
  14. if(in_array($end, $allowed_ext)){
  15. $name = date("Ymd").time();
  16. $location = 'video/'.$name.".".$end;
  17. if(move_uploaded_file($file_temp, $location)){
  18. mysqli_query($conn, "INSERT INTO `video` VALUES('', '$name', '$location')") or die(mysqli_error());
  19. echo "<script>alert('Video Uploaded')</script>";
  20. echo "<script>window.location = 'index.php'</script>";
  21. }
  22. }else{
  23. echo "<script>alert('Wrong video format')</script>";
  24. echo "<script>window.location = 'index.php'</script>";
  25. }
  26. }else{
  27. echo "<script>alert('File too large to upload')</script>";
  28. echo "<script>window.location = 'index.php'</script>";
  29. }
  30. }
  31. ?>
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.

Enjoy Coding!

Comments

Submitted byVEpixl (not verified)on Sun, 05/22/2022 - 15:35

I tried this on nginx and uploaded to my server, all works besides video upload doesn't move into the server, what could be the issue? does it have to run on apache?
Submitted bybatsinda (not verified)on Mon, 02/13/2023 - 19:31

it dint work for sure
Submitted byMTH (not verified)on Fri, 04/14/2023 - 13:33

Thank you ..

Add new comment