General Community > Scripting Help

php question about brackets ()

(1/3) > >>

ApplianceJunk:
Code is for "Saving the uploaded file" at the bottom of this page.
http://w3schools.com/php/php_file_upload.asp


--- Code: ---<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_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("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";
  }
?>

--- End code ---

Think I have a fair understanding what's going on here in the above code as I can add file types so I can upload .pdf file, text file, different files sizes etc..
But I just can't get my head wrapped around how it's decided how many brackets are used and why.

Example, in this line there are three brackets before the $_FILES and one at the end.


--- Code: ---if ((($_FILES["file"]["type"] == "image/gif")

--- End code ---

and there are two brackets at the end of this line.

--- Code: ---|| ($_FILES["file"]["type"] == "image/pjpeg"))

--- End code ---

and this line also has two brackets at the end of it.

--- Code: ---&& ($_FILES["file"]["size"] < 20000))

--- End code ---

Now if I go and remove one of the three brackets at the beginning of this line...

--- Code: ---if ((($_FILES["file"]["type"] == "image/gif")

--- End code ---

I get a syntax error.

--- Quote ---[27-May-2012 17:16:36] PHP Parse error:  syntax error, unexpected T_BOOLEAN_AND in /Applications/MAMP/htdocs/php/upload_file.php on line 5
--- End quote ---

Yet I can add a line of code such as the line below so I can upload .pdf file, but I don't have to add any additional brackets any other place.

--- Code: ---|| ($_FILES["file"]["type"] == "application/pdf")

--- End code ---

So I'm just not seeing why the number of brackets in the locations they are at.

Maybe someone could clear things up a bit for me.

Thanks,


Yoshi:
You know maths right? What's in the brackets first? Same in PHP.

If you do:

--- Code: ---if ($test && ($testing == 'test'))
--- End code ---

PHP will do whatever is in the last brackets ($testing == 'test' in this case). So if $testing is indeed 'test', the code will become:

--- Code: ---if ($test && true)
--- End code ---

Then PHP will execute the last bit.

W3 is using that, taking this as example:

--- Code: ---if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
--- End code ---

Simplifying that up real quick:

--- Code: ---(($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/pjpeg") && $_FILES["file"]["size"] < 20000)
--- End code ---

It basically checks whether the file type is an image first (if it's just one of them it returns true) and then checks the file size.
So if my image is a GIF, JPEG or JPJPEG (?), it makes this code:

--- Code: --- if (true  && $_FILES["file"]["size"] < 20000)
--- End code ---

However when it's PNG, it returns this:

--- Code: --- if (false && $_FILES["file"]["size"] < 20000)
--- End code ---
Since PNG is not in that list of || (or)'s.

Hope you understand this a bit, I suck at explaining :P

ApplianceJunk:
Seems I'm simply looking at sets of brackets that are located in another set of brackets then.

Yoshi:

--- Quote from: ApplianceJunk on May 27, 2012, 07:04:42 PM ---Seems I'm simply looking at sets of brackets that are located in another set of brackets then.

--- End quote ---
Correct :)

ApplianceJunk:
I also learned from you that the || means (or)'s.
Kind of had a idea, but had not ready it any place yet.

Thanks,

Navigation

[0] Message Index

[#] Next page

Go to full version