Jump to content

Stuck on PHP form for videos


Libellula

Recommended Posts

Hello! So I am completely new to the PHP side of the house, so forgive me if I am making very simple basic errors here.. the learning process has been selective. I am trying to create a simple video upload form. I have set restrictions on the types of files the user can upload. I have sucessfully got it to work uploading images, but not videos. I don't know what I am doing wrong?

 

Form Code:

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

PHP Script:

<?php
$allowedExts = array("avi", "mpg", "mov");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "video/avi")
|| ($_FILES["file"]["type"] == "video/mpg")
|| ($_FILES["file"]["type"] == "video/mov"))
&& ($_FILES["file"]["size"] < 100000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    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";
  }
?> 

Original PHP Script that worked for images:

<?php
$allowedExts = array("jpg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 100633448)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    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";
  }
?> 

 

As I said, I am new to this... I figured if the image only version worked, the video only version would work if I changed the extionsions. I changed image/ to video/, just to see if that was why it didn't work. No luck either way. Hoping someone can figure this out! I would so appriciate the assistance. :)

Link to comment
https://forums.phpfreaks.com/topic/276270-stuck-on-php-form-for-videos/
Share on other sites

Okay, I have added those you listed to be allowed in all the formats you listed. Same error, "Invalid File".

 

<?php
$allowedExts = array("avi", "mpg", "mov");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/x-troff-msvideo")
|| ($_FILES["file"]["type"] == "video/avi")
|| ($_FILES["file"]["type"] == "video/msvideo")
|| ($_FILES["file"]["type"] == "video/mpg")
|| ($_FILES["file"]["type"] == "video/mpeg")
|| ($_FILES["file"]["type"] == "video/quicktime"))
&& ($_FILES["file"]["size"] < 100633448)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    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";
  }
?> 

Infact, you should use var_dump instead. You have too many checks in one place, you should be doing them sequentially.

 

First which should be checked is for file upload errors $_FILES["file"]["error"]

Then file size, then file type, then any other checks.

No, I didn't. W3 Schools site had the framework for an image uploader, so I was hoping I could adapt it similarly for video. As I said I am brand new to PHP. :P This specific function is just something one of my clients absolutly needs on their site and it is the only thing I do not know how to do. Frustrating!

I've given you a good start with this, I usually wouldn't as this is a paid project for you.

 

 

<?PHP

  $allowedMimes = array('application/x-troff-msvideo',
                        'video/avi',
                        'video/msvideo',
                        'video/x-msvideo',
                        'video/mpeg',
                        'video/quicktime');

  if(!is_uploaded_file($_FILES['file']['tmp_name'])) {
    echo 'Possible file upload attack.';
  } else if($_FILES['file']['error'] > 0) {
    echo 'File upload error. Error Code: '. $_FILES['file']['error'];              
  } else if(!in_array($_FILES['file']['type'], $allowedMimes)) {
    echo 'File type not permitted.';
  } else if($_FILES['file']['size'] > 100633448) {
    echo 'File is too big.';
  } else {
    //## Checks passed, do file moving
  }
 
?>

 

You can do the rest from what I've given you above.

 

*Edit - I made a few errors, so I've fixed them.

Thank you! So you know, I am doing this without pay for a non-profit organization that supports school fine arts programs. That is why I'm trying to do it, instead of finding someone to hire - as thier isn't really a budget. I really appriciate the time and help. I will try your above soultion out.

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.