Jump to content

Upload Problem


SkyRanger
Go to solution Solved by SkyRanger,

Recommended Posts

I keep receiving the error Invalid file when uploading 

 

this is my form

<form method="post" action="upload_imfile.php" enctype="multipart/form-data">
<label>File to Upload:</label>
<input type="file" name="file">
<input type="submit" name="submit" value="Proceed with Upload">
</form>

 

 

$allowedExts = array("pdf", "zip", "rar");
$extension = end(explode(".", $_FILES["file"]["name"]));

if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/zip")
|| ($_FILES["file"]["type"] == "application/x-zip-compressed")
|| ($_FILES["file"]["type"] == "multipart/x-zip")
|| ($_FILES["file"]["type"] == "application/x-compressed")
|| ($_FILES["file"]["type"] == "application/octet-stream"))
&& ($_FILES["file"]["size"] < 20000)
&& 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("clients/$client/personal/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "clients/$client/personal/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "clients/$client/personal/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }

 

 

Link to comment
Share on other sites

You don't need to wrap each condition in parens.

Unless I am missing something, I don't see anything obvious that stands out in the if condition.

What debugging steps have you taken so far? Be sure to validate each conditions' value.

Link to comment
Share on other sites

You don't need to wrap each condition in parens.

There's nothing wrong with wrapping each condition in parens, as it clearly documents the programmers intentions. I wrap ALL of my conditions in parens, in essence telling the interpreter/compiler exactly what I mean, so we don't misunderstand each other.

 

 

  if ($_FILES["file"]["error"] > 0)
    {

 

 

This condition should really be tested FIRST! Before using any other elements of the $_FILES array, you need to verify that you actually received a file. If I try to upload a 2G file and your PHP/server fails it because it exceeds the max allowed size, this is the only element of the array that is meaningful.

 

 

Since your current error message is generic and used if ANY condition fails, it is hard to know why. You may want to printf('<PRE>%s</PRE>', htmlspecialchars(print_r($_FILES, true))); to have a look at the $_FILES array and see why it reached that point (well, at least during development). Or you may want to break up your tests and produce a message telling why the file is invalid.

Link to comment
Share on other sites

There's nothing wrong with wrapping each condition in parens, as it clearly documents the programmers intentions. I wrap ALL of my conditions in parens, in essence telling the interpreter/compiler exactly what I mean, so we don't misunderstand each other.

 

No there is nothing wrong with it, but wrapping each condition in parens AND writing them each on a new line is overkill.

The interpreter is not a person, it does not misunderstand. If that is your style then more power to you but I believe that causes more typing then what is needed. The only instance where you need parens in a condition is when you need to specify the order of precedence.

Off topic I know, but you called me out. :P

Link to comment
Share on other sites

Thank you for that rudimentary lesson Barand.

This topic isn't worth any more then one more post as it is menial.

What I was trying to point out is that this:

 

 

if((something == something || something == something) && something == something)

 

is the exact same as:

 

 

if(((something == something) || (something == something)) && (something == something)))

 

only is much more confusing to look at.

 

parens around a single condition:

 

 

(something == something)

 

is not needed.

 

To use your example Barand:

 

 

 

(A AND B) OR C
 

 

 

same as:

 

 

 

((A) AND (B)) OR (C)
 

 

Edit: What's with the double spacing?

Edited by AyKay47
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.