Jump to content

Upload Problem


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
https://forums.phpfreaks.com/topic/275281-upload-problem/
Share on other sites

  On 3/5/2013 at 6:34 PM, AyKay47 said:

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.
  On 3/5/2013 at 6:18 PM, SkyRanger said:

 

 

  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
https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416802
Share on other sites

  On 3/5/2013 at 8:57 PM, DavidAM said:

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
https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416808
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?

Link to comment
https://forums.phpfreaks.com/topic/275281-upload-problem/#findComment-1416816
Share on other sites

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.