Video Upload

Hi to all you helpful people here at sitepoint, I keep putting this to the back burner, but now that I’ve had so much help with other problems that are now all sorted out I thought it was time to address this once again. Firstly is it possible to upload a video file to the same machine i.e. I’m running windows 7 with wamp server as my testing or development server and I want to upload a video file from a second hard drive, can that be done? I know theres a ton of tutorials out there, to show how to do this, however, I just simply can’t get it to work. I’ve changed the php.ini file to allow up to 500mb both the post data size and max allowed file size, there may be other setting in the php.ini file that need to change as well but I don’t know what. The best I’ve been able to get is a blank page loaded with no errors and nothing echoed and definetly no file in the upload directory. As I’ve hacked most of the tutorials to bits, trying to get it to work, I don’t have any php to post here, But I do have the form that I’m using, maybe one of you gurus might be able to write me something for it, here it is

<form method=“post” action=“videoupload.php” enctype=“multipart/form-data”/>
<input type=‘hidden’ name=‘MAX_FILE_SIZE’ value=‘500000’ />
<input type=“hidden” name=“FILE_FORMAT” value=“mp4” />
<input type=“file” name=“upload” size=‘20’ />
<input type=“submit” class=“style1” value=“Upload” />
</form>

And as always any help is greatly appreciated

So after you attempt to upload have your tried copying the the tmp file to a new location or it just doesn’t work? This process should not care whether it is uploading it to the same or remote machine. I have this working on the same machine, but I use Linux, so it may be different.

Have you output $_FILES by using var_dump($_FILES) after you post this should look something like:

array(1) { [“upload”]=> array(5) { [“name”]=> string(11) “video.mp4” [“type”]=> string(15) “application/mp4” [“tmp_name”]=> string(14) “/tmp/php4fdiQJ” [“error”]=> int(0) [“size”]=> int(20318) }}

If you see something similar in your output then you can copy the file using something like:

if($_FILES['upload']['name'] != ''){
        $target_path = "/path/to/upload/folder/";
        $target_path = $target_path . basename( $_FILES['upload']['name']);
        if(move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) { //Move files to your target directory from the tmp directory
             $msg = "The file <strong>".  basename( $_FILES['upload']['name']). "</strong> has been uploaded";
        } else{ //Failure
             $msg = "There was an error uploading the file  ".  basename( $_FILES['upload']['name']). ", please try again!";
        }
}
?>

Hope this helps.

Steve

Thanx for the reply Server Storm, yes I have tried moving the tmp file with very similar code to what you just posted, but I haven’t used the var_dump($_FILES) as its not something I’m very familiar with, only come across it once before, and I didn’t understand it back then, this is all very new to me, as I’m trying to teach myself. However, I’ll copy this across and get back to you soon.Cheers

Ok I just got “array empty” and an error undefined index upload on this line “if($_FILES[‘upload’][‘name’] != ‘’){” .Cheers

I’ve managed to get the undefined index error fixed by changing the above php code to what I’ve posted underneath, however, the var_dump($_FILES) says the array is still empty, I did some googling and found out that the post data size needs to be greater than the max allowed file size, so I corrected that by increasing the post data size by 100mb, but still no file uploaded and array empty, what needs to be done to get array to show a string?
Here’s the code I changed.

<?php

ini_set(‘display_errors’,1);
error_reporting(E_ALL);

var_dump($_FILES);

if ($_FILES)//[‘upload’][‘name’] != ‘’)
{
$name = $_FILES[‘upload’][‘name’];
$target_path = “upload/”;
$target_path = $target_path . basename( $_FILES[‘upload’][‘name’]);
if(move_uploaded_file($_FILES[‘upload’][‘tmp_name’], $target_path)) { //Move files to your target directory from the tmp directory
$msg = “The file <strong>”. basename( $_FILES[‘upload’][‘name’]). “</strong> has been uploaded”;
} else{ //Failure
$msg = "There was an error uploading the file ". basename( $_FILES[‘upload’][‘name’]). “, please try again!”;
}
}
?>

Hi,

I have modified your code and managed to get it working for you:

<html>
<head>
</head>
<body>

<form method="post" action="upload.php" enctype="multipart/form-data"/>
	<input type='hidden' name='MAX_FILE_SIZE' value='500000' />
	<input type="hidden" name="FILE_FORMAT" value="mp4" />
	Choose file to upload:<input type="file" name="upload" size='20' />
	<input type="submit" name="submit" value="Upload" />
</form>
<?php

ini_set('display_errors',1);
error_reporting(E_ALL);

if(isset($_POST['submit'])) {

if(!empty($_FILES['upload']) && ($_FILES['upload']['error'] == 0))	{
	$name = $_FILES['upload']['name'];
	$target_path = "upload/"; //PUT IN THE PATH WHERE YOU WANT THE UPLOADED FILE TO GO.
	$target_path = $target_path . basename( $_FILES['upload']['name']);
	
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) { //Move files to your target directory from the tmp directory
	echo "The file <strong>". basename( $_FILES['upload']['name']). "</strong> has been uploaded";
} else { //Failure
	$msg = "There was an error uploading the file ". basename( $_FILES['upload']['name']). ", please try again!";
}

} else {
	echo 'No file was uploaded.';
}

}

?>

</body>
</html>

This code is by no means secure and still needs more work but I’ll leave that up to you.

Hi Codarz360, thankyou very much for your effort, but unfortunately it still does not work, you seem to have written this as if all the code is on the same page, it is not, the html form is on a completely seperate page to the php. And when I added the var_dump($_FILES); to your code I’m still getting array empty!!! Nothing echoed and no uploaded video. It’exactly the same as if I was still using my code. Cheers

Ok I have now split the pages up into two separate ones.

form.html file:

<html>
<head>
</head>
<body>

<form method="post" action="upload.php" enctype="multipart/form-data"/>
	<input type='hidden' name='MAX_FILE_SIZE' value='500000' />
	<input type="hidden" name="FILE_FORMAT" value="mp4" />
	Choose file to upload:<input type="file" name="upload" size='20' />
	<input type="submit" name="submit" value="Upload" />
</form>

</body>
</html>

Upload.php file

<?php

ini_set('display_errors',1); 
error_reporting(E_ALL); 

var_dump($_FILES);

if(isset($_POST['submit'])) {

if(!empty($_FILES['upload']) && ($_FILES['upload']['error'] === 0))	{
	$name = $_FILES['upload']['name'];
	$target_path = ""; //PUT IN THE PATH WHERE YOU WANT THE UPLOADED FILE TO GO.
	$target_path = $target_path . basename($name);

if(move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) { //Move files to your target directory from the tmp directory
	echo "The file <strong>". basename($name). "</strong> has been uploaded";
} else { //Failure
	$msg = "There was an error uploading the file ". basename($name). ", please try again!";
}
} else {
	echo 'No file was uploaded.';
}
}
?>

That is working perfectly for me and is showing the array.

Thanx Codarz360 but I put it into 2 pages when I tested it before, I don’t think the problem is with the code, I think it might be more to do with the php.ini file or maybe permissions or something like that, it really beats me as to why it shows the array as empty. Cheers mate.

Ok here is a link to everything that is in my PHP.ini file on my WAMP server http://pastebin.com/rZLsMgmn just copy and paste that over yours and test to see if it works correctly.

Your upload_max_filesize = 2M. That is somewhat small for video file. Perhaps you need to set it to a larger number.

Firstly, what version of php is this? Secondly, how big a file have you been using to try this with as your max allowed file size is still set at 2mb where I have mine set to 500mb and I’ve been trying to upload an 18mb file, as its getting rather late here now, I won’t be back to reply until the morning. Cheers mate.

Beat me to it tom8 .Cheers mate

I’ve just been uploading a small text file that is why it is still set to 2MB. It’s been working fine with the text file and maybe you want to try it using a text file to test your settings are correct.

You do realize your form’s max file size is set to 500k, not 500M, right? That number is measured in B, not KB.

So then that would mean that in order to equal 500MB you would need to set the form value to 524,288,000 bytes?

correct.

I think that this should solve your problem then studentofIT.

Changed the value of the MAX_FILE_SIZE to 524288000 bytes which should now allow you to upload your file.


<html>
<head>
</head>
<body>

<form method="post" action="upload.php" enctype="multipart/form-data"/>
	<input type='hidden' name='MAX_FILE_SIZE' value='524288000' />
	<input type="hidden" name="FILE_FORMAT" value="mp4" />
	Choose file to upload:<input type="file" name="upload" size='20' />
	<input type="submit" name="submit" value="Upload" />
</form>

</body>
</html>

Added in a file-type check.


<?php

ini_set('display_errors',1); 
error_reporting(E_ALL); 

if(isset($_POST['submit'])) {

if(!empty($_FILES['upload']) && ($_FILES['upload']['error'] === 0))	{
	$name = $_FILES['upload']['name'];
	$target_path = ""; //PUT IN THE PATH WHERE YOU WANT THE UPLOADED FILE TO GO.
	$target_path = $target_path . basename($name);
	
if($_FILES['upload']['type'] == 'video/mp4') {	

if(move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) { //Move files to your target directory from the tmp directory
	echo "The file <strong>". basename($name). "</strong> has been uploaded";
} else { //Failure
	$msg = "There was an error uploading the file ". basename($name). ", please try again!";
}
} else {
	echo 'Incorrect file type. Must be .mp4';
}
} else {
	echo 'No file was uploaded.';
}
} else {
	header('location: form.html');
}

?>

I’ve tested it out now with a 6.75mb .mp4 file and everything seems to working ok.

Thanx StarLion, I was wondering about that, I did have it set at 500mb at one stage, but I think I changed it back again before I tested, Codarz360 there is no need to keep posting the same code over again, as there is nothing wrong with my code, except for the max file size limit as just mentioned by StarLion, I will be using that. Now all I have to do is change it and test it. Be back soon with the results. Cheers very muchly guys.

Ok changing the form to 500000000 bytes had no effect whatsoever, however, I did what Codarz360 suggested and tried 2 smaller files, seperately of course, the first one was a .txt file, the size was just 49bytes, and the second was a microsoft word document, the size was 2.22mb, the .txt file gave this
array
‘upload’ =>
array
‘name’ => string ‘Rockbits URL.txt’ (length=16)
‘type’ => string ‘text/plain’ (length=10)
‘tmp_name’ => string ‘C:\wamp\ mp\phpC5C5.tmp’ (length=23)
‘error’ => int 0
‘size’ => int 49
but still no file was uploaded and nothing echoed, the word document gave this
array
‘upload’ =>
array
‘name’ => string ‘All great websites have a great server behind them2.docx’ (length=56)
‘type’ => string ‘’ (length=0)
‘tmp_name’ => string ‘’ (length=0)
‘error’ => int 1
‘size’ => int 0
once again no file uploaded and nothing echoed here either, obviously the word document didn’t even get to the tmp dir and showing an error with no size at all, the .txt file made it to the tmp dir with no errors and there wasn’t a problem with it recognisining the size, so beats me as to whats going on.