0

I am having an issue with undefined error in my php. After research I know I know I should use isset but I'm not sure how to implement it. Here is the peice of code that causes the issue:

if (($_FILES["file"]["type"] == "video/mp4") && ($_FILES["file"]["size"] < 20000))
      {

Should I put the isset inside the if function before each $_FILES or where should it go ?

Here is the full piece of code, as you will probably notice I am quite new to PHP:

$ivalid_file='0';

    if (($_FILES["file"]["type"] == "video/mp4") && ($_FILES["file"]["size"] < 20000))
      {
      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("uploads/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "uploads/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "invalid_file";
      }
0xtuytuy
  • 1,494
  • 6
  • 26
  • 51
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Rizier123 Mar 01 '15 at 06:11
  • What are you trying to achive with this code? – Amit Verma Mar 01 '15 at 06:13
  • @AmitThakur He just wants to know how to check if $_FILES["file"] exists or not – littleibex Mar 01 '15 at 06:13
  • I saw this page but I am not sure how to actually implement it in my piece of code. Sorry for such an ignorant question. – 0xtuytuy Mar 01 '15 at 06:14

1 Answers1

2

As far as I understand, you want to know how to check if the file was ever uploaded and how to implement it in code. All you gotta do is, wrap you entire code in an if block which checks if $_FILES['files'] is set like so:

//Check if "file" exists here:
if(isset($_FILES["file"])) {
$ivalid_file='0';

    if (($_FILES["file"]["type"] == "video/mp4") && ($_FILES["file"]["size"] < 20000))
      {
      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("uploads/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "uploads/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "invalid_file";
      }
} else {
// Display appropriate error
}
littleibex
  • 1,705
  • 2
  • 14
  • 35
  • no explanation..., how should OP be able to solve this next time by himself? Also how should he know what you changed/fixed and what it does?! – Rizier123 Mar 01 '15 at 06:18