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
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";
  }
?> 
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Edited by PaulRyan
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.