Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi Larry,

I used your add_pdf.php script from Chapter 5 as template for an add_video.php script (below) to upload MP4 videos instead of PDF files (I only changed the PDF variable to MP4 per your script). The script executes perfectly fine and the validation works on all files except .MOV files. When I test with a .MOV upload, the script prints the following errors:

An error occurred in script 'C:\xampp\htdocs\site\html\add_video.php' on line 39: Undefined index: mp4

An error occurred in script 'C:\xampp\htdocs\site\html\add_video.php' on line 39: Trying to access array offset on value of type null

An error occurred in script 'C:\xampp\htdocs\site\html\add_video.php' on line 91: Undefined index: mp4

An error occurred in script 'C:\xampp\htdocs\site\html\add_video.php' on line 92: Trying to access array offset on value of type null

I have search for a solution but could not find anything on Stack Overflow or any other source. Any suggestions would be appreciated.

Thank you.

<?php

// This page is used by an administrator to add a video to the site.

// Require the configuration before any PHP code as the configuration controls error reporting:
require('./includes/config.inc.php');

// If the user isn't logged in as an administrator, redirect them:
redirect_invalid_user('user_admin');

// Require the database connection:
require(MYSQL);

// Include the header file:
$page_title = 'Add a Video';
include('./templates/header.html');

// For storing errors:
$add_video_errors = array();

// Check for a form submission:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {	

	// Check for a title:
	if (!empty($_POST['title'])) {
		$t = escape_data(strip_tags($_POST['title']), $dbc);
	} else {
		$add_video_errors['title'] = 'Please enter the title!';
	}
	
	// Check for a description:
	if (!empty($_POST['description'])) {
		$d = escape_data(strip_tags($_POST['description']), $dbc);
	} else {
		$add_video_errors['description'] = 'Please enter the description!';
	}

	// Check for a mp4:
	if (is_uploaded_file($_FILES['mp4']['tmp_name']) && ($_FILES['mp4']['error'] === UPLOAD_ERR_OK)) {
	
		// Get a reference:
		$file = $_FILES['mp4'];
		
		// Find the size:
		$size = ROUND($file['size']/1024);

		// Validate the file size (5MB max):
		if ($size > 3072) {
			$add_video_errors['mp4'] = 'The uploaded file was too large.';
		} 

		// Validate the file type:
		// Create the resource:
		$fileinfo = finfo_open(FILEINFO_MIME_TYPE);

		// Check the file:
		if (finfo_file($fileinfo, $file['tmp_name']) !== 'video/mp4') {
			$add_video_errors['mp4'] = 'The uploaded file was not an mp4.';
		} 

		// Close the resource:
		finfo_close($fileinfo);

		// Move the file over, if no problems:
		if (!array_key_exists('mp4', $add_video_errors)) {

			// Create a tmp_name for the file:
			$tmp_name = sha1($file['name']) . uniqid('',true);
	
			// Move the file to its proper folder but add _tmp, just in case:
			$dest =  VIDEOS_DIR . $tmp_name . '_tmp';

			if (move_uploaded_file($file['tmp_name'], $dest)) {
	
				// Store the data in the session for later use:
				$_SESSION['mp4']['tmp_name'] = $tmp_name;
				$_SESSION['mp4']['size'] = $size;
				$_SESSION['mp4']['file_name'] = $file['name'];
	
				// Print a message:
				echo '<div class="alert alert-success"><h3>The file has been uploaded!</h3></div>';
	
			} else {
				trigger_error('The file could not be moved.');
				unlink ($file['tmp_name']);				
			}

		} // End of array_key_exists() IF.
	
	} elseif (!isset($_SESSION['mp4'])) { // No current or previous uploaded file.
		switch ($_FILES['mp4']['error']) {
			case 1:
			case 2:
				$add_video_errors['mp4'] = 'The uploaded file was too large.';
				break;
			case 3:
				$add_video_errors['mp4'] = 'The file was only partially uploaded.';
				break;
			case 6:
			case 7:
			case 8:
				$add_video_errors['mp4'] = 'The file could not be uploaded due to a system error.';
				break;
			case 4:
			default: 
				$add_video_errors['mp4'] = 'No file was uploaded.';
				break;
		} // End of SWITCH.

	} // End of $_FILES IF-ELSEIF-ELSE.
	
	if (empty($add_video_errors)) { // If everything's OK.
		
		// Add the video to the database:
		$fn = escape_data($_SESSION['mp4']['file_name'], $dbc);
		$tmp_name = escape_data($_SESSION['mp4']['tmp_name'], $dbc);
		$size = (int) $_SESSION['mp4']['size'];
		$q = "INSERT INTO videos (title, description, tmp_name, file_name, size) VALUES ('$t', '$d', '$tmp_name', '$fn', $size)";
		$r = mysqli_query($dbc, $q);
		if (mysqli_affected_rows($dbc) === 1) { // If it ran OK.
	
			// Rename the temporary file:
			$original =  VIDEOS_DIR . $tmp_name . '_tmp';
			$dest =  VIDEOS_DIR . $tmp_name;
			rename($original, $dest);

			// Print a message:
			echo '<div class="alert alert-success"><h3>The MP4 has been added!</h3></div>';

			// Clear $_POST:
			$_POST = array();

			// Clear $_FILES:
			$_FILES = array();

			// Clear $file and $_SESSION['mp4']:
			unset($file, $_SESSION['mp4']);
					
		} else { // If it did not run OK.
			trigger_error('The MP4 could not be added due to a system error. We apologize for any inconvenience.');
			unlink ($dest);
		}
				
	} // End of $errors IF.
	
} else { // Clear out the session on a GET request:
	unset($_SESSION['mp4']);	
} // End of the submission IF.

// Need the form functions script, which defines create_form_input():
require('includes/form_functions.inc.php');
?><h1>Add a Video</h1>
<form enctype="multipart/form-data" action="add_video.php" method="post" accept-charset="utf-8">

	<input type="hidden" name="MAX_FILE_SIZE" value="5242880">
	
	<fieldset><legend>Fill out the form to add a video to the site:</legend>
	
<?php
create_form_input('title', 'text', 'Title', $add_video_errors); 
create_form_input('description', 'textarea', 'Description', $add_video_errors); 

// Add the file input:
echo '<div class="form-group';

// Add classes, if applicable:
if (array_key_exists('mp4', $add_video_errors)) {
	echo ' has-error'; 
} else if (isset($_SESSION['mp4'])) {
	echo ' has-success'; 
}
echo '"><label for="mp4" class="control-label">Add MP4 file</label><input type="file" name="mp4" id="mp4">';

// Check for an error:
if (array_key_exists('mp4', $add_video_errors)) {
	
	echo '<span class="help-block">' . $add_video_errors['mp4'] . '</span>';
	
} else { // No error.

	// If the file exists (from a previous form submission but there were other errors),
	// store the file info in a session and note its existence:		
	if (isset($_SESSION['mp4'])) {
		echo '<p class="lead">Currently: "' . $_SESSION['mp4']['file_name'] . '"</p>';
	}

} // end of errors IF-ELSE.
echo '<span class="help-block">MP4 only, 3MB Limit</span>
</div>';

?>
		<input type="submit" name="submit_button" value="Add This Video" id="submit_button" class="btn btn-default" />
	
	</fieldset>

</form> 

<?php // Include the HTML footer:
include('./templates/footer.html');
?>

 

Edited by Jacques

Share this post


Link to post
Share on other sites

My guess would be the file isn't being uploaded. How big is the .mov file you're testing with? And have you checked your server settings related to handling uploads (such as the max POST size)?

Share this post


Link to post
Share on other sites

Hi Larry,

Thank you very much for your response. And yes, you "guess" was correct of course!

The "Undefined index: mp4" error only appears when the the file size, including an MP4, is much larger that the "upload_max_filesize" and the "post_max_size" as defined in the php.ini file (currently set at 10MB each). Trying to uploading a 20MB file (double the maximum set values) still generates only a form validation error. But any file from 50MB and upwards generates the "Undefined index: mp4" error.

Any way to avoid the "Undefined index" error (without increasing the "upload_max_filesize" and the "post_max_size" values) just in case a user tries to upload a 50MB plus video file?

Regards, 

Share this post


Link to post
Share on other sites

You could do an isset($_FILES['mp4']['tmp_name']) first or just use the error suppression operator on the is_uploaded_file(). 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...