Upload Video File to Server/FTP with Form?

Hello,
I’m trying to set up my form to upload video files to a file on the server. I cant find any help on video files and uploading to the server. Ive found a few picture to email forms but that is all. Does anyone know how to do this?

I’m trying to use this code but the file doesn’t show up on the FTP, and I get an error every time after changing the file types from jpg, png, ect. to avi, mp4.

[php]<?php
$allowedExts = array(“avi”, “mp4”, “mov”, “flv”, “jpg”);
$temp = explode(".", $_FILES[“file”][“name”]);
$extension = end($temp);
if ((($_FILES[“file”][“type”] == “videos/avi”)
|| ($_FILES[“file”][“type”] == “videos/mp4”)
|| ($_FILES[“file”][“type”] == “videos/mov”)
|| ($_FILES[“file”][“type”] == “videos/flv”)
|| ($_FILES[“file”][“type”] == “videos/jpg”)
|| ($_FILES[“file”][“type”] == “videos/png”))
&& in_array($extension, $allowedExts))
{
if ($_FILES[“file”][“error”] > 0)
{
echo "Return Code: " . $_FILES[“file”][“error”] . “
”;
}
else
{
echo "Upload: " . $_FILES[“file”][“name”] . “
”;
echo "Type: " . $_FILES[“file”][“type”] . “
”;
echo “Size: " . ($_FILES[“file”][“size”] / 1024) . " kB
”;
echo "Temp file: " . $_FILES[“file”][“tmp_name”] . “
”;

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
}

}
else
{
echo “Invalid file”;
}
?> [/php]

I got it to say successful but the file never appears online.

[php]<?php
if ($_FILES[“file”][“error”] > 0)
{
echo "Return Code: " . $_FILES[“file”][“error”] . “
”;
}
else
{
echo "Upload: " . $_FILES[“file”][“name”] . “
”;
echo "Type: " . $_FILES[“file”][“type”] . “
”;
echo “Size: " . ($_FILES[“file”][“size”] / 1024) . " kB
”;
echo "Stored in: " . “videos/” . $_FILES[“file”][“name”]. “
”;

echo “Invalid file”;
}
?> [/php]

This will upload an image but gives me an error when uploading a MP4 file. What could be stopping the video format?

Current PHP:
[php]<?php
$allowedExts = array(“mp4”, “jpeg”, “jpg”, “png”);
$temp = explode(".", $_FILES[“file”][“name”]);
$extension = end($temp);
if ((($_FILES[“file”][“type”] == “video/mp4”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/jpg”)
|| ($_FILES[“file”][“type”] == “image/pjpeg”)
|| ($_FILES[“file”][“type”] == “image/x-png”)
|| ($_FILES[“file”][“type”] == “image/png”))
&& ($_FILES[“file”][“size”] < 2000000000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“file”][“error”] > 0)
{
echo "Return Code: " . $_FILES[“file”][“error”] . “
”;
}
else
{
echo "Upload: " . $_FILES[“file”][“name”] . “
”;
echo "Type: " . $_FILES[“file”][“type”] . “
”;
echo “Size: " . ($_FILES[“file”][“size”] / 1024) . " kB
”;
echo "Temp file: " . $_FILES[“file”][“tmp_name”] . “
”;

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
}

}
else
{
echo “Invalid file”;
}
?>
[/php]

try to dump the files[file] array on error and see if the values (name/type) are as expected.

I edited the code to this but it still gets denied.

[php]<?php
$allowedExts = array(“mp4”, “mpeg”, “jpg”, “MPEG-4”, “jpeg”, “avi”);
$temp = explode(".", $_FILES[“name”][“name”]);
$extension = end($temp);
if ((($_FILES[“name”][“type”] == “video/mp4”)
|| ($_FILES[“name”][“type”] == “image/jpeg”)
|| ($_FILES[“name”][“type”] == “image/jpg”)
|| ($_FILES[“name”][“type”] == “video/avi”)
|| ($_FILES[“name”][“type”] == “video/mpeg”)
|| ($_FILES[“name”][“type”] == “video/MPEG-4”))
&& ($_FILES[“name”][“size”] < 2000000000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“name”][“error”] > 0)
{

}

else
{

if (name_exists("upload/" . $_FILES["name"]["name"]))
  {
  echo $_FILES["name"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_name($_FILES["name"]["tmp_name"],
  "upload/" . $_FILES["name"]["name"]);
  echo "Thank you for your video submission!</h2><br>" . "upload/" . $_FILES["name"]["name"];
  }
}

}
else
{
echo “Invalid name”;
}
?>
[/php]

Like this I get a success but the file does not get uploaded.

[php]<?php

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Thank you for your video submission!<br>" . "upload/" . $_FILES["file"]["name"];
  }

?> [/php]

add this to the top and paste the output here
[php]echo ‘

’;
var_dump($_FILES);
die();[/php]

Thank you, I’m not sure if I added it like you were saying but I get an error but its a code not “invalid file”. I changed all the text with “file” to “name” but I am seeing [“name”][“name”] witch doesn’t seem right to me.

Here is what my code looks like:
[php]<?php
echo ‘

’;
var_dump($_FILES);
$allowedExts = array(“mp4”, “mpeg”, “jpg”, “MPEG-4”, “jpeg”, “avi”);
$temp = explode(".", $_FILES[“name”][“name”]);
$extension = end($temp);
if ((($_FILES[“name”][“type”] == “video/mp4”)
|| ($_FILES[“name”][“type”] == “image/jpeg”)
|| ($_FILES[“name”][“type”] == “image/jpg”)
|| ($_FILES[“name”][“type”] == “video/avi”)
|| ($_FILES[“name”][“type”] == “video/mpeg”)
|| ($_FILES[“name”][“type”] == “video/MPEG-4”))
&& ($_FILES[“name”][“size”] < 2000000000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“name”][“error”] > 0)
{
}

else
{

if (name_exists("upload/" . $_FILES["name"]["name"]))
  {
  echo $_FILES["name"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_name($_FILES["name"]["tmp_name"],
  "upload/" . $_FILES["name"]["name"]);
  echo Thank you for your video submission!" . "upload/" . $_FILES["name"]["name"];
  }
}

}
else
{
echo “Invalid file”;
}
die();
?> [/php]

Please paste the output of the script as it is, try to upload the mp4-file and it should throw out all the information about it.

Im sorry about that.
Here is the output.

[php]array(1) {
[“file”]=>
array(5) {
[“name”]=>
string(9) “Bobbi.mp4”
[“type”]=>
string(0) “”
[“tmp_name”]=>
string(0) “”
[“error”]=>
int(1)
[“size”]=>
int(0)
}
}[/php]

Thank you for your help. I noticed that when I’m using the code that was changed to name the images get invalid file as well, with file the images would upload. Could this be part of the problem?

as you can see you do not have $_FILES[“name”][“name”] or $_FILES[“name”][“type”], so your conditions will result in false

[php]($_FILES[“name”][“type”] == “video/mp4”)

will be executed as

(null == “video/mp4”)[/php]

you should do a foreach, even though you are only expecting one file (since $_FILES contains an array of uploaded files)

[php]foreach ($_FILES as $file) {

$temp = explode(".", $file[‘name’]);
$extension = end($temp);

// etc[/php]

you can also note that $file[‘type’] is empty, so the second check on type will not work.

I replaced the code with what you provided but it is still giving me an invalid file :’(

[php]<?php
$temp = explode(".", $file[‘name’]);
$extension = end($temp);
if ((($_FILES[“name”][“type”] == “video/mp4”)
|| ($_FILES[“name”][“type”] == “image/jpeg”)
|| ($_FILES[“name”][“type”] == “image/jpg”)
|| ($_FILES[“name”][“type”] == “video/avi”)
|| ($_FILES[“name”][“type”] == “video/mpeg”)
|| ($_FILES[“name”][“type”] == “video/MPEG-4”))
&& ($_FILES[“name”][“size”] < 2000000000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“name”][“error”] > 0)
{

}

else
{

if (name_exists("upload/" . $_FILES["name"]["name"]))
  {
  echo $_FILES["name"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_name($_FILES["name"]["tmp_name"],
  "upload/" . $_FILES["name"]["name"]);
  echo "Thank you for your video submission!" . "upload/" . $_FILES["name"]["name"];
  }
}

}
else
{
echo “go back”;
}
?>
[/php]

With this code:
[php]<?php
echo ‘

’;
var_dump($_FILES);
die();
$allowedExts = array(“mp4”, “mpeg”, “jpg”, “MPEG-4”, “jpeg”, “avi”);
$temp = explode(".", $_FILES[“file”][“name”]);
$extension = end($temp);
if ((($_FILES[“file”][“type”] == “video/mp4”)
|| ($_FILES[“file”][“type”] == “image/jpeg”)
|| ($_FILES[“file”][“type”] == “image/jpg”)
|| ($_FILES[“file”][“type”] == “video/avi”)
|| ($_FILES[“file”][“type”] == “video/mpeg”)
|| ($_FILES[“file”][“type”] == “video/MPEG-4”))
&& ($_FILES[“file”][“size”] < 2000000000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES[“file”][“error”] > 0)
{
}

else
{

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_file"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Thank you for your video submission!<br>" . "upload/" . $_FILES["file"]["name"];
  }
}

}
else
{
echo “Invalid file”;
}
?>

[/php]

The image upload works:
[php]array(1) {
[“file”]=>
array(5) {
[“name”]=>
string(14) “1330547302.jpg”
[“type”]=>
string(10) “image/jpeg”
[“tmp_name”]=>
string(14) “/tmp/php1xQcU1”
[“error”]=>
int(0)
[“size”]=>
int(6762)
}
}[/php]

The video upload still shows this:
[php]array(1) {
[“file”]=>
array(5) {
[“name”]=>
string(9) “Bobbi.mp4”
[“type”]=>
string(0) “”
[“tmp_name”]=>
string(0) “”
[“error”]=>
int(1)
[“size”]=>
int(0)
}
}[/php]

I don’t understand why the video file is invalid but the image file perfect…

I would just leave out checking for file type, it’s just a value submitted by the browser and worthless for any security. Just do your first check on file extension and you should be good to go.

Apperantly your browser does not know what file type the .mp4 is, hence php does not get a file type submitted.

Sponsor our Newsletter | Privacy Policy | Terms of Service