The Video Upload Page

Now that we have created the workhorses of our application, and set up some common template files, we need only create the frontman scripts to take all the glory. Here we will talk about the upload page and related view templates.

Create a new php file in your public root "/" named "upload.php" and add the following content to it.


$errors = array();
$uploadComplete = false;

if (isset($_POST['submit'])) {
	$post = $_POST;
	$file = isset($_FILES['video']) ? $_FILES['video'] : null;

	if ($file['name'] == null)
		$errors['video'] = 'you must select a video to upload';

	// test Db connection
	if (!dbConnect()) {
		throw new Exception('Database error: ' . mysqli_error());

	// if no errors, process that shizzle
	if (count($errors) == 0) {

		// instantiate VideoConverter class
		$converter = new VideoConverter($file, $config);

		$details = $converter->getDetails();

		if ($videoId = insertVideo($details)) {

			if ($converter->processVideo()) {

				$uploadComplete = true;

			} else {



$pageTitle = "Upload A Video";


Here we require our "config.ini.php" file, which gives us access to all our database functions, and we also require the VideoConveter.php Class as we will be needing it on this page to process our video.

We create an errors array to hold an error should someone try to submit the form with no file selected, as well as a variable $uploadComplete which will act as a flag to tell our template script some info about what it should display.

Next we check if our form has been submitted, store the post in a variable and do the same with the $_FILES['video'] which contains information on the uploaded file. We check to see whether the $files "name" key is empty and if so set an error that the user should have selected a file to upload.

After checking our database connection and ensuring there are no errors, we instantiate the VideoConverter Class passing in the file from the form and the $config array from our configuration file. The Class will do all the work for us, so all we have to do is get the details about the upload from it and execute the conversion.

We retrieve the video details from the class with $converter->getDetails() and store this information in a variable which we use to insert the data into the database. If the database insert is successful we process the video, and if that is successful we set the $uploadComplete flag to true, otherwise we delete the video row from the database and fall through.

Finally we set a page title for our template file and get the template file, which we will create now. Inside "/templates/" create a file named "upload.php" and add the following content.

<div class="center">
	<h2 class="mainHeading">Upload A video</h2>

<div id="uploadForm">

if ($uploadComplete) {

Theres not a whole lot there. What it does is include our header and footer, as well as our upload form, and depending on our $uploadComplete flag it will include our uploadResults.php Template. First lets take a look at the upload form.

Create a file in "/templates/" named uploadForm.php and add the following content to it.

<form action="upload.php" method="post" enctype="multipart/form-data">
	<legend>Select a Video to Upload</legend>
	<div class="formRow">
		<label for="video">Select Video</label>
		<input type="file" name="video" />
		<?php if(isset($errors['video'])):?>
			<div class="error">
				<?php echo $errors['video'];?>
		<?php endif;?>
<div class="submit">
	<input type="submit" name="submit" value="Upload Video!" />

As you can see this is just an html form with one little php snippet to display an error message if it is set.

Next we will create the php file "uploadResults.php" in "/templates/".

<div id="uploadResults">

	<h3>Video Conversion Results</h2>

	<div class="videoThumbnail">
		<a href="/video.php?video=<?php echo $videoId;?>">
			<img src="/data/video/thumbnails/<?php echo $details['thumbnail'];?>" />

			<strong>Video Length: <?php echo $details['duration'];?></strong>


This template will be displayed if the $uploadComplete variable is set to true. It will display the video thumbnail as a link to view the video as well as the video duration. Notice the target address of the link is a file named video.php and we pass it a parameter named video with the value set to the video id in the db table.

That is the meat and potatoes ladies and gentlemen. The rest is just display and presentation. Next we will take a look at displaying a particular video.