Solved

Adding a video upload to a client website

Posted on 2009-05-18
5
220 Views
Last Modified: 2012-05-07
I have a client who is interested in having a video page on their website which they themselves will be able to upload videos to and possibly categorize them so that visitors can browse them easily. What method do you think is best? I can code it from scrach in PHP, but I am sure there are better solutions, even if they cost me.

Any advice would be great!
0
Comment
Question by:swtwebdesign
  • 3
  • 2
5 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24416421
Uploading videos is essentially the same as uploading photos, and I can show you how I do that in this code snippet.  Obviously you will want to raise the "max file size" and change the allowable file types, but the rest of the principles apply.

As far as categorizing them, that would seem to be best done with a simply MySQL table.  It would contain the uploaded file name and a "tagging keyword" or phrase.  A search of this table for a keyword would then give you the links to the uploaded videos that were tagged with the appropriate information.

Does that help? ~Ray
<?php // RAY_upload_example.php

error_reporting(E_ALL);
 

// MANUAL REFERENCE PAGES

// http://docs.php.net/manual/en/features.file-upload.php

// http://docs.php.net/manual/en/features.file-upload.common-pitfalls.php

// http://docs.php.net/manual/en/function.move-uploaded-file.php

// http://docs.php.net/manual/en/function.getimagesize.php
 

// ESTABLISH THE NAME OF THE 'uploads' DIRECTORY

$uploads = 'uploads';
 

// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT

$max_file_size = '8192000';  // EIGHT MEGABYTE LIMIT ON UPLOADS
 

// ESTABLISH THE KINDS OF FILE EXTENSIONS WE CAN ACCEPT

$file_exts = array('jpg', 'gif', 'png', 'txt');
 

// ESTABLISH THE NUMBER OF FILES WE CAN UPLOAD

$nf = 3;
 
 
 

// THIS IS A LIST OF THE POSSIBLE ERRORS THAT CAN BE REPORTED in $_FILES[]["error"]

$errors	= array(

	0 => "Success!",

	1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",

	2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",

	3 => "The uploaded file was only partially uploaded",

	4 => "No file was uploaded",

	6 => "Missing a temporary folder",

	7 => "Cannot write file to disk"

);
 
 
 

// IF THERE IS NOTHING IN $_POST, PUT UP THE FORM FOR INPUT

if (empty($_POST))

{

	?>

	<h2>Upload <?=$nf?> file(s)</h2>
 

	<!--

		SOME THINGS TO NOTE ABOUT THIS FORM...

		NOTE THE CHOICE OF ENCTYPE IN THE HTML FORM STATEMENT

		MAX_FILE_SIZE MUST PRECEDE THE FILE INPUT FIELD

		INPUT NAME= IN TYPE=FILE DETERMINES THE NAME YOU FIND IN $_FILES ARRAY

	-->
 

	<form name="UploadForm" enctype="multipart/form-data" action="<?=$_SERVER["REQUEST_URI"]?>" method="POST">

	<input type="hidden" name="p" value="1" />

	<input type="hidden" name="MAX_FILE_SIZE" value="<?=$max_file_size?>" />

	<p>

	Find the file(s) you want to upload and click the "Upload" button below.

	</p>
 

	<?php for ($n = 0; $n < $nf; $n++)

		{

			echo "<input name=\"userfile$n\" type=\"file\" size=\"80\" /><br/>\n";

		}

	?>

	<br/>Check this box <input autocomplete="off" type="checkbox" name="overwrite" /> to <b>overwrite</b> existing files.

	<input type="submit" name="_submit" value="Upload" />

	</form>

	<?php

	die();

}
 
 
 

else // WE HAVE GOT SOMETHING IN $_POST

{
 

// THERE IS POST DATA - PROCESS IT

	echo "<h2>Results: File Upload</h2>\n";
 

// ACTIVATE THIS TO SEE WHAT IS COMING THROUGH

//	echo "<pre>"; var_dump($_FILES); var_dump($_POST); echo "</pre>\n";
 

// ITERATE OVER THE CONTENTS OF $_FILES

	foreach ($_FILES as $my_uploaded_file)

	{
 

// SKIP OVER EMPTY SPOTS - NOTHING UPLOADED

		$error_code	= $my_uploaded_file["error"];

		if ($error_code == 4) continue;
 

// SYNTHESIZE THE NEW FILE NAME

		$f_type	= trim(strtolower(end    (explode( '.', basename($my_uploaded_file['name'] )))));

		$f_name	= trim(strtolower(current(explode( '.', basename($my_uploaded_file['name'] )))));

		$my_new_file = getcwd() . '/' . $uploads . '/' . $f_name .'.'. $f_type;

		$my_file     = $uploads . '/' . $f_name .'.'. $f_type;
 

// OPTIONAL TEST FOR ALLOWABLE EXTENSIONS

		if (!in_array($f_type, $file_exts)) die("Sorry, $f_type files not allowed");
 

// IF THERE ARE ERRORS

		if ($error_code != 0)

		{

			$error_message = $errors[$error_code];

			die("Sorry, Upload Error Code: $error_code: $error_message");

		}
 

// GET THE FILE SIZE

		$file_size	= number_format($my_uploaded_file["size"]);
 

// MOVE THE FILE INTO THE DIRECTORY

// IF THE FILE IS NEW

		if (!file_exists($my_new_file))

		{

			if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))

			{

				$upload_success = 1;

			}

			else

			{

				$upload_success = -1;

			}
 

// IF THE FILE ALREADY EXISTS

		}

		else

		{

			echo "<br/><b><i>$my_file</i></b> already exists.\n";
 

// SHOULD WE OVERWRITE THE FILE? IF NOT

			if (empty($_POST["overwrite"]))

			{

				$upload_success = 0;
 

// IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP

			}

			else

			{

				$now	= date('Y-m-d');

				$my_bak = $my_new_file . '.' . $now . '.bak';

				if (!copy($my_new_file, $my_bak))

				{

					echo "<br/><b>Attempted Backup Failed!</b>\n";

				}

				if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))

				{

					$upload_success = 2;

				}

				else

				{

					$upload_success = -1;

				}

			}

		}
 

// REPORT OUR SUCCESS OR FAILURE

		if ($upload_success == 2) { echo "<br/>It has been overwritten.\n"; }

		if ($upload_success == 1) { echo "<br/><b><i>$my_file</i></b> has been saved.\n"; }

		if ($upload_success == 0) { echo "<br/><b>It was NOT overwritten.</b>\n"; }

		if ($upload_success < 0)  { echo "<br/><b>ERROR <i>$my_file</i> NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND</b>\n"; }

		if ($upload_success > 0)

		{

			echo "$file_size bytes uploaded.\n";

			if (!chmod ($my_new_file, 0755))

			{

				echo "<br/>chmod(0755) FAILED: fileperms() = ";

				echo substr(sprintf('%o', fileperms($my_new_file)), -4);

			}

			echo "<br/><a href=\"$my_file\">See the file $my_file</a>\n";

		}

// END ITERATOR

	}

}

?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24981953
Thanks for the points - it's a very useful line of inquiry! ~Ray
0
 

Author Comment

by:swtwebdesign
ID: 24983780
That code was very helpful, however I am stuck on file size limitations. The files I need to upload are 200MB+ and nothing I try works.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24983896
File upload size can be controlled  in PHP.  See: http://us.php.net/manual/en/features.file-upload.errors.php and http://us.php.net/manual/en/ini.core.php#ini.upload-max-filesize.  However it can also be throttled by your hosting company and very large files can cause timeouts if the data pipe is slow.  If you have the right values in PHP (use phpinfo() to check), I would approach the hosting company.  ~Ray
0
 

Author Comment

by:swtwebdesign
ID: 24993913
I finally got it figured out. Thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now