• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to Upload Video Files using PHP

jackbrooo

Zero-Knowledge Proof Pro
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
131
Likes
131
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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.

2018-09-20_11_25_52-localhost_127.0.0.1_db_video_phpmyadmin_4.7.0.png

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. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

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. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `video` ORDER BY `video_id` ASC"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  23. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  24. ?>
  25. <div class="col-md-12">
  26. <div class="col-md-4" style="word-wrap:break-word;">
  27. <br />
  28. <h4>Video Name</h4>
  29. <h5 class="text-primary"><?php

    echo

    $fetch

    [

    'video_name'

    ]

    ?>

    </h5>
  30. </div>
  31. <div class="col-md-8">
  32. <video width="100%" height="240" controls>
  33. <source src="<?php

    echo

    $fetch

    [

    'location'

    ]

    ?>

    ">
  34. </video>
  35. </div>
  36. <br style="clear:both;"/>
  37. <hr style="border-top:1px groovy #000;"/>
  38. </div>
  39. <?php
  40. }
  41. ?>
  42. </div>
  43. <div class="modal fade" id="form_modal" aria-hidden="true">
  44. <div class="modal-dialog">
  45. <form action="save_video.php" method="POST" enctype="multipart/form-data">
  46. <div class="modal-content">
  47. <div class="modal-body">
  48. <div class="col-md-3"></div>
  49. <div class="col-md-6">
  50. <div class="form-group">
  51. <label>Video File</label>
  52. <input type="file" name="video" class="form-control-file"/>
  53. </div>
  54. </div>
  55. </div>
  56. <div style="clear:both;"></div>
  57. <div class="modal-footer">
  58. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  59. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  60. </div>
  61. </div>
  62. </form>
  63. </div>
  64. </div>
  65. <script src="js/jquery-3.2.1.min.js"></script>
  66. <script src="js/bootstrap.js"></script>
  67. </body>
  68. </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. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  5. $file_name

    =

    $_FILES

    [

    'video'

    ]

    [

    'name'

    ]

    ;
  6. $file_temp

    =

    $_FILES

    [

    'video'

    ]

    [

    'tmp_name'

    ]

    ;
  7. $file_size

    =

    $_FILES

    [

    'video'

    ]

    [

    'size'

    ]

    ;

  8. if

    (

    $file_size

    <

    50000000

    )

    {
  9. $file

    =

    explode

    (

    '.'

    ,

    $file_name

    )

    ;
  10. $end

    =

    end

    (

    $file

    )

    ;
  11. $allowed_ext

    =

    array

    (

    'avi'

    ,

    'flv'

    ,

    'wmv'

    ,

    'mov'

    ,

    'mp4'

    )

    ;
  12. if

    (

    in_array

    (

    $end

    ,

    $allowed_ext

    )

    )

    {
  13. $name

    =

    date

    (

    "Ymd"

    )

    .

    time

    (

    )

    ;
  14. $location

    =

    'video/'

    .

    $name

    .

    "."

    .

    $end

    ;
  15. if

    (

    move_uploaded_file

    (

    $file_temp

    ,

    $location

    )

    )

    {
  16. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `video` VALUES('', '$name

    ', '$location

    ')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  17. echo

    "<script>alert('Video Uploaded')</script>"

    ;
  18. echo

    "<script>window.location = 'index.php'</script>"

    ;
  19. }
  20. }

    else

    {
  21. echo

    "<script>alert('Wrong video format')</script>"

    ;
  22. echo

    "<script>window.location = 'index.php'</script>"

    ;
  23. }
  24. }

    else

    {
  25. echo

    "<script>alert('File too large to upload')</script>"

    ;
  26. echo

    "<script>window.location = 'index.php'</script>"

    ;
  27. }
  28. }
  29. ?>

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!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,500

350,639

350,649

Top