How to Create a Video Manager in PHP & MySQL


In this tutorial, we show how to create a video manager in PHP & MySQL, kind of like a video manager that you would see on youtube.

With a video manager, users can manage videos that they upload to a site. A video manager may include options such as the ability to delete a video that has been uploaded, view a detailed view of the video so that a user can change the title of the video, and other advanced features such as the ability to edit videos.

In this tutorial, we just take a simple approach and show how to allow a user to delete videos that they have uploaded.

We provide this interface through the use of check boxes next to each video that has been uploaded. A user can then check the box and click the 'Delete' button in order to delete the video that has been checked off.

Again, video sites such as youtube, make wide use of this feature.

Since this is a simple tutorial, we will not be creating a video manager with all the advanced features of youtube, but give a basic start to how video managers can be constructed.

Below is a video uploader in which a user can upload videos and manage the videos to be able to delete uploaded videos.

Not quite how it would be in real life, but just serves as an educational tool.

Below is a video upload form. Give the video a description and then upload the video. You will see it appear in the table below.

Note that this video uploader only accepts the video formats mp4, ogg, and webm. All other formats will not work.

Just as a note, I created a website, that allows for users to upload any type of files that they want and share it with the rest of the world. The site accepts video uploads, audio uploads, image uploads, pdfs, word documents...anything. It's like youtube but it's for any type of file. Like youtube, registration is free and uploading files is free. The files uploaded can be any size. There are no restrictions regarding size. Any files that are uploaded and made public get put in all the search engines. Signup can be done quickly either through a form or through a social account, google or facebook. The below form is just for demonstration purposes. If you want files to be hosted permanently, then go to the link above in this paragraph.

Description of Video:


In order to insert videos into a MySQL database, as explained before, we don't actually put the videos in the database. We put the videos in a regular folder on our website. I created a videos folder (or directory) on my website which the video files will upload to. In the MySQL database, all you do is place the video name along with the file extension of the video. We also place a description of the video in the database. However, to display the video, all we need is the file name of the video and the file extension. Once we have these, all we have to do in the PHP code is specify the full path to this video, so that it can be displayed and played.

So, again, we upload the video to a directory on our website. We don't put the video directly in the MySQL database. We upload it to a directory on our site. We simply get the video file name and file extension of the video and place it in the MySQL database. We then have the complete pathway to the video in our code so that we can show and play it.

So if we're saving a video named, movies.mp4, we save the full file name, moviews.mp4 and we also save the file extension of the video, which is mp4.

We will go through this below.

In this demonstration, I'm going to use phpMyAdmin. When creating a table in MySQL, we create 4 columns.

One column is the ID column, which should be present in really any MySQL table. This column is unique and should be set to autoincrement. It counts sequentially 1 up from any new row of data inserted. So it starts at 1. If you insert 5 rows of data, the fifth row will have an ID of 5. The purpose of it is that it's unique and it gives you a way to order the new uploads in order of when they were uploaded. You could also do this in other ways. But this is probably the easiest way of showing the videos in order from when they were uploaded.

The second column that I created was a description column. This holds a description of the video being entered. Once the user puts a description of the video in the text box and uploads the video along with the description, the description of the video entered will be inserted into the descripton column of the table.

The third column that I created is the filename column. This holds the full file name of the video. So if you're uploading a video named basketballgame.mp4, the full file name entered into the filename column is basketballgame.mp4

The fourth and last column that I created is the fileextension column. I created a dedicated column for the file extension because we play the videos using the HTML video tag (<video>). With this tag, you must specify the file extension of the video. Therefore, it's best to create a column that holds the file extension for each video. There are other ways you could get the file extension, but this is the best and surest way to do so.

Below you can see the structure of the table created that serves as the video uploads table.

Since it holds video information, I named the table, 'Videos'. You can see the image of the structure of the table below.

Video uploads table structure

So you can see the structure of the table above.

The ID column is of type int (11).

The description column is set to VARCHAR(100) to allow it to hold a good deal of characters. Sometimes the user may want to put in a somewhat good description of the video, so we allow up to 100 characters.

The filename column is set to VARCHAR(50) to allow for a somewhat long filename.

The file extension is set to VARCHAR(4), since file extensions can be up to 4 characters long.

The reason we use VARCHAR for all these fields is because it allows a variable amount of characters. CHAR is a fixed amount. You would use CHAR when every upload is the same amount of characters. This would be the case for a few things such as state abbreviations, since they are all 2 characters. In this case, CHAR wouldn't work for any of the fields. Even the file extension can vary between 3 characters (mp4) and 4 characters (webm).

Of course, if you need to, modify the amount parameters to satisfy your needs.


So now let's go over the HTML code needed to create the uploader seen above, which allows for videos to be uploaded.

The HTML code is shown below.

The HTML code above creates an upload form. Since we want this page to keep all information obtained from the form, we set action equal to "". If we wanted to send the information entered into this uploader to another page, we would specify that PHP page. But since we want the information for later PHP code on this page, we set action equal to nothing. The method is POST. And for file uploading, the statement, enctype="multipart/form-data" must be input. If not, file uploading will not work.

The line underneath is a line that creates a text box, which holds the description of the video. If no description is entered and the user clicks the Upload button, the statement, "A description must be entered" is output.

We then create a line allowing for the file upload. If no file is entered, the statement, "Please choose a file" is output.

We then create a submit button, as pretty much every form needs. We give it the value, "Upload", so that Upload appears on the button. If not, it would just have submit by default.

We then close the form.

This concludes the HTML needed for this page.

PHP Code

First Block of PHP Code

There are 3 blocks of PHP codes needed.

The first block is shown below.

This block is for uploading the file entered to the directory, where we will store the video, in this case this is the Uploads/videos/ directory.

So this code takes the file that the user entered. Once entered, we are able to find out the file extension of the file by looking for the "." in the file. What follows after this will be the file extension, such as mp4, webm, or ogg.

We also take the description that the user entered of the video and place it in the $description variable.

We specify the directory that we want the video uploaded to. In this case, it is the Upload/videos/ directory. If the user has clicked the Upload button without specifying a file, the statement, "Please choose a file" is output. If the user enters a file but it is not of the format, mp4, ogg, or web, the statement, "The file extension must be .mp4, .ogg, or .webm in order to be uploaded" is output. If the user enters a file and it is mp4, ogg, or webm, the file is uploaded to the directory we specified through the move_uploaded_file() function. This function creates a temporary name for the file upload and then permanently transfers the file to the directory we specify. The statement, "Uploaded" is then output.

This concludes this block of PHP code.

Second Block of PHP Code

The next block of PHP code inserts the data into the MySQL table for storage. These include the description of the video, the filename of the video, and the file extension of the video.

This block of code is shown below.

This block of code establishes connection to the MySQL database being used. This requires a user, password, host, database, and table name.

As stated above, a MySQL table was created with phpMyAdmin. This table has 4 columns: ID, description, filename, fileextension.

If the description text box is not empty, then we insert into the Videos MySQL table the description, filename, and fileextension of the video.

We then close the database.

Third Block of PHP Code

The next block of PHP code displays the table with the video description and actual video, as well as provides the check boxes next to the videos and creates the Delete button at the end of display of videos, so that a user can delete uploaded videos.

So again, we establish connection to the MySQL database which contains the Videos table.

We query the table containing the video information.

We then display the table with the html <table> tag.

The statement, while ($row = mysql_fetch_array($result)) gives a loop so that every row in the accounted for. As long as there are rows, the statement, mysql_fetch_array($result) will be true, so the loop won't be exited until all rows are output.

Using the $row['value'] feature, we are able to get all of the information from each of the columns for each row. So we obtain the description, filename, and file extension for each row, which represents a video.

In the first column of the table, we add what in PHP is an array of check boxes. With an array of check boxes, we can get all videos that have been checked off by the user for video deletion.

We call the name attribute of this check box array 'videoids[]'. We then create a PHP variable called $videodelete. This variable holds an array of all of the videos selected.

The approach that I took for this check box array was to set the values of each of the check boxes to the id of the video. Because an id is unique to each video, an id is a unique identifier for each video. By setting the value of the check box to the id of the video, this ensures that each video is represented uniquely.

I then did a foreach loop, so that I looped through each id in the $videodelete variable. If the $videodelete variable is set (checked through the isset() function), then this means that the user has checked off videos. In this case, I use a MySQL delete function to delete those selected videos. I then output if the video has been successfully deleted or not.

And that's the approach that I took for this uploading videos.

I would always use ids to reference videos, because ids are unique to each video.

And this concludes all the code which is necessary for building a video uploader and manager with PHP and MySQL.

Again, for a site like youtube, there are many other things and features you could add. This video manager also isn't practical because it is not linked to a user. You would have a website that has users log in and then based on the login, you would have a video manager unique to each user and the videos that they have uploaded. But this would make a tutorial such as this much more advanced, so we just stuck to the basics here.

But this is just a simple rundown of how to create a video manager in PHP and MySQL>

Related Resources

How to Insert Images into a MySQL Database Using PHP

How to Insert Files into a MySQL Database Using PHP

How to Select a Table from a MySQL Database

How to Create a Confirmation Page for an HTML Web Form Using PHP

How to Redirect to Another URL with PHP

How to Create a Search Engine Using PHP

How to Upload Images to a Website Using PHP

How to Upload Files to a Website Using PHP

How to Create a Searchable Database Using MySQL and PHP

How to Search a MySQL Table For Any Word or Phrase Using PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Register and Login Page Using PHP

HTML Comment Box is loading comments...