Jump to content

MP4 Upload Script


Nightasy

Recommended Posts

Greetings all, 

 

So I'm trying to set up a video upload page and I can't seem to get it to upload the video.

 

Here's the script.

<?php
//Start the Session.
session_start();
//Set variable for page title prior to loading header which needs variable.
$pagetitle = 'Home Page';
//Set variable for css file prior to loading header which needs variable.
$navstyle = 'includes/styles/responsivemobilemenu.css';
//Set variable for css file prior to loading header which needs variable.
$pagestyle = 'includes/styles/firstlevel.css';
//Set variable for responsive css prior to loading header which needs variable.
$pageboiler = 'includes/styles/boilerplate.css';

//Add in header.
include ('includes/header.php');
//Requires the database connection script.
require ('masters/connect.php');
?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<fieldset>
<legend>Select an mp4 video file:</legend>
<br/>
<label for="file">Select a video: </label><input type="file" name="upload" />
<br/>
<label for="vidname">Video Name: </label><input type="text" name="vidname" size="25" maxlength="25"
value="<?php if(isset($_POST['vidname'])) echo $_POST['vidname'];?>"/>
<p><b>Video Description</b></p> 
<textarea name="viddesc" cols="40" rows="10" maxlength="255">
<?php if(isset($_POST['viddesc'])) echo $_POST['viddesc'];?></textarea>
<br/>
<div class="buttonHolder">
<br/>
<input type="submit" name="submit" value="Begin Upload" />
</div>
<br/>
</fieldset>
</form>

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$errors = array();
	
/*	// Check that video file is selected and mp4.
	if (!isset($_FILES['upload'])) {
	$errors[] = 'You forgot to select a video for upload.';
	} else {
		$allowed = array("video/mpeg");
		if (in_array($_FILES['upload']['type'], $allowed)) {
			// Get extension to variable. (Added this in case I want to allow other formats in future.)
			$oldvidname = $_FILES['upload']['name'];
			$ext = substr($oldvidname,strpos($oldvidname, '.'));
		} else {
			$errors[] = 'The file you selected is not an mp4!';
		}
	}
*/	
	// Check for a video name:
	if (empty($_POST['vidname'])) {
	$errors[] = 'You forgot to enter a name for your video.';
 	} else {
		$vidname = mysqli_real_escape_string($connect, trim($_POST['vidname']));
		$vidnamecheck = "SELECT * from videofiles WHERE vidname='$vidname'";
		$vidresult = mysqli_query($connect,$vidnamecheck);
		$resultvidcheck = mysqli_num_rows($vidresult);
		if ($resultvidcheck > 0){
			// If vid name exists display message.
			$errors[] = 'The video name you entered is already being used!';}
	}
	
	// Check for a video description.
	if (empty($_POST['viddesc'])) {
	$errors[] = 'You forgot to give the video a description!';	
	} else {
		$viddesc = mysqli_real_escape_string($connect, trim($_POST['viddesc']));
	}
	
	// If no errors upload video and add to database, else report errors.	
	if (empty($errors)) {
		// Upload Script Begin
		$oldvidname = $_FILES['upload']['name'];
		$ext = substr($oldvidname,strpos($oldvidname, '.'));
		
		
		$newviddest = 'videos/' . $vidname . $ext;
		$newvidname = $vidname . $ext;
		move_uploaded_file($_FILES['upload']['tmp_name'], $newviddest);
			// Display success messaage.
			echo "<p class='message'>Your video has uploaded successfully.</p>";
		
		
		
		$query = "INSERT INTO videofiles (vidname, viddesc)
				  VALUES ('$newvidname','$viddesc')";
				  
		
		if (!mysqli_query($connect,$query)){
  			die('Error: ' . mysqli_error($connect));
   		}
		
		// Close database connection.
		mysqli_close($connect);
		// Delete the file if it still exists:
		if (file_exists ($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name']) ) {
			unlink ($_FILES['upload']['tmp_name']);
		}
	
		
	} else {
		echo '<div id="errorbox">';
		foreach ($errors as $msg) {
			// Print each error.
			echo " - $msg<br />\n";
			}
			echo '</p><p>Please try again.</p>';
			echo '</div>';
	}	
}
?>

<?php
include ('includes/footer.php');
?>

Ignore the validation check for the file selection. I'm having issues there as well and I commented it out. I plan to take a look at that once I can get the file to even upload.

 

The script adds the fields into the database just fine but the video doesn't upload to the videos folder. Any help is greatly appreciated.

 

Best Regards,

Nightasy

Link to comment
Share on other sites

Actually, never mind. This script does work, well, it has issues that need worked out but it definitely uploads to the server just fine.

 

Apparently this works on the live server but not on my local testing server.

 

Thanks anyhow to anyone that looked it over, yes I know it needs more work. 8D

Link to comment
Share on other sites

@mac_gyver - Yea, I get what you're saying. The issue actually wasn't even the upload script though, it had something to do with WAMP not being happy with me moving a file on my computer. Not exactly sure what causes that issue but the script does work when I test it on my live server. It uploads to the live server and works without a hiccup, which means that last night I spent several hours trying to fix a script that wasn't even broken in the first place. In the future I will definitely test upload scripts on the remote server instead of on my testing server. I have a secure testing environment setup with a password login on my remote live server anyhow that I put into place just for this type of issue.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.