How to Upload Videos to a Website Using PHP
In this article, we show how to upload videos to a website using PHP.
With this upload feature, you upload videos to whatever folder you specify in the code. Once you have a video selected to upload, you press the 'Upload' button. The video is then uploaded to the folder that you can specify. You can then display this video by the HTML <video> tag.
This is exactly what is done above.
We created a file upload box in HTML and then we added PHP to give functionality to the textbox so that an actual upload can occur. Once we have the video uploaded, we can then display the video through some simple PHP code. So if you upload a video above, then click the 'Upload' button, you will see the video you uploaded displayed right below.
We added some code to the PHP so that only certain types of files can be uploaded. This allows the upload form to only accept files that are video files. For this video upload form, we do not want other types of files, such as image files or pdfs to allowed to be uploaded. We only want files that are videos; thus, they will have video extensions. For example, certain files are always videos, such as .mov, .mp4, and .avi. Other type of files such as text files (.txt), pdf files (.pdf), word files (.doc) are not videos. Therefore, this file upload would reject them. We want only videos to be uploaded. So we added this PHP code so that files, which are definitely not videos, cannot be uploaded.
The video upload form accepts only .mp4, .ogg, and .webm files. It rejects all other types of files.
So below we go over how to do this.
HTML Code
So below we will start with the HTML code needed to create the file upload form.
So the HTML shown above is very basic.
We create a form, a file upload form. We set the action equal to "". This means that the form doesn't redirect to another page but keeps all the information on this page. We set the method equal to POST, because it's a large amount of data and we don't want the data appended to a URL. We want the data in an actual location, which is where we're uploading the video file to. The enctype statement is unique and needed for file uploads. It must be present.
The next line creates a file upload button. We name it "file".
The next line creates a submit button. Since it's an upload button, we name it "Upload".
We then end the form.
This is all the HTML code needed. The HTML simply creates the upload form.
The PHP code shown below is what actually adds functionality to the upload form so that it actually performs
an upload.
PHP Code
There are 2 PHP blocks of code to make this video upload work.
The first and the biggest block is shown below.
So this is obviously a big block of code. We'll explain it all now below.
The $name variable stores the name of the file being uploaded. This includes the name of the file plus its extension. So if upload a video named Vacation and its of the format mpeg, the $name variable stores Vacation.mpeg.
The $tmp_name stores the temporary name of the file. When we first upload the file, it isn't carried over directly to the final destination, which is the folder we want it transferred to. Instead, it gets a temporary. We then have to use the function, move_uploaded_file() to transfer the file using the temporary name to its final destination folder. So, again, the variable $tmp_name is very important for uploading the file.
The next variable we have is $position. This variable holds the position of the period in the file name. We find the position of the period, because after the period is the file extension. Once we know the position of the period, we know what comes after the period is the file extension. We want to know the file extension to know whether the file is a video or not. The video files that this upload form accepts are mp4, ogg, and webm file formats. Any other file extension isn't accepted by this upload form.
So we have the variable position, which we find with the strpos() function. Next we use the substr() function and get the full extension. The substr() function takes the part of a string after a certain point. So we take the part of the string after the period. This gives us the file extension. We store this value in the variable $fileextension.
Next, we make the file extension lower case. We do this because when we later use if statements to determine whether the file is an acceptable video file or not, we just have to compare lowercase characters, without having to worry whether the file extension may be capitalized or not. This makes the if statements straightforward.
The variable $path makes the video file uploaded to the videos folder. Later on, we just have to append to the videos folder the file name of the video so that we establish the complete pathway to the video file.
The line, if (isset($name)) checks to see whether the submit button has been clicked. If it has been clicked, the $name variable will be set.
We then use the line, if (empty($name)), to determine once the submit button has been clicked whether the $name variable contains anything or not. If the user simply pressed the 'Upload' button without specifying a file, the $name variable will be empty. If the user pressed the 'Upload' button while specifying a file, the $name variable will have something.
So if the $name variable is empty, no file has specified by the user. So we output the statement, "Please choose a file".
Else, if the $name variable is not empty but the file extension isn't jpg, jpeg, png, or bmp, we output the statement, "The file extension must be .jpg, .jpeg, .png, or .bmp in order to be uploaded".
Else, if the $name variable is not empty and the file extension is either jpg, jpeg, png, or bmp, we output the statement, "Uploaded". During this stage, we use the function, (move_uploaded_file($tmp_name, $path.$name)) to upload the file using the temporary name created to its permanent location. Its permanent location will be its current folder, in the videos folders. Right now, you're viewing the website from, http://www.learningaboutelectronics.com/Articles/
So the video will be located in the videos folder in the Upload directory of the Articles directory.
This completes the first block of PHP code.
The second block of PHP code is shown below.
So this block of PHP code is much smaller.
If the file extension is either mp4, ogg, or webm, the video uploaded will be shown.
The video is displayed through the <video> </video> tag. We set the width to 320, since this will allow it to be displayed on all types of devices, including the smallest mobile phones. We give the video controls, so that we can vary volume, size, the play button, etc. The src of the video is the complete pathway to the video file. So the full pathway to the video file being uploaded on this website is Articles/Uploads/Videos/videofile.
If the video tags do not work, the line, Your browswer does not support the video tag.
The video tag used in HTML is still relatively new. It hasn't been around forever. In fact, prior to this tag created with HTML5, HTML could not be used to display a video. Instead, Flash would have to be used. Now, with HTML5, with the video tag, videos can be shown with HTML.
The browsers that support the rendering of the HTML5 video tag are Chrome 4.0, Internet Explorer 9.0, Firefox 3.5, Safari 4.0, and Opera 10.5. If you are using any of these respective browsers prior to these conditions, the browser may be unable to render the video using the video tag. So keep this in mind if you are using an older browser. The video will be displayed for prior versions of these browsers.
Realize that with the HTML video tag, the only 3 video formats supported so far are .mp4, .ogg, and .webm file formats. This is why we've included these 3 formats with this video uploader. No other video formats are supported as of this writing, so they won't work.
So this is all that is required to upload videos and display them. It isn't particularly hard once you know what you're doing.
You can always modify the code to fix your needs. For example, you may only want to accept mp4 formats. In that case, you would erase the lines pertaining to the other file extensions. You may want to add more video formats, because there are plenty of others available. In that case, you would just add them to the code.
If you want to see how to restrict the size of the file being uploaded, see
How to Restrict the Size of a File Upload with PHP.
Related Resources
How to Insert Videos Into a MySQL Database Using PHP
How to Upload Images to a Website Using PHP
How to Upload Files to a Website Using PHP
How to Insert Images into a MySQL Database Using PHP