0

I got a really annoying error. After an > my script ends.

<html>
<body>
    <h1>Upload file to see previous upload!</h1>
    <form action="index.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
            if (isset ($_FILES["file"])) {
                $allowedExts = array("jpeg", "jpg", "png");
                $temp = explode(".", $_FILES["file"]["name"]);
                $extension = end($temp);
                if ((($_FILES["file"]["type"] == "image/jpeg")
                  || ($_FILES["file"]["type"] == "image/jpg")
                  || ($_FILES["file"]["type"] == "image/pjpeg")
                  || ($_FILES["file"]["type"] == "image/x-png")
                  || ($_FILES["file"]["type"] == "image/png"))
                  && ($_FILES["file"]["size"] < 1048576)
                  && in_array($extension, $allowedExts)) {
                    if ($_FILES["file"]["error"] > 0) {
                        echo "Error: Couldn't upload file!";
                    } else {
                        move_uploaded_file($_FILES["file"]["tmp_name"], "files/" . $_FILES["file"]["name"]);
                    }
                } else {
                    echo "Upload a file NAO!";
                }
            }
        ?>
</body>
</html>

So on my page I can read this: "0) { echo "Error: Couldn't upload file!"; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "files/" . $_FILES["file"]["name"]); } } else { echo "welcome!"; } } ?> "

Well, guess what! That was not my intention to do... So if someone can explain me why this is happening I will be thankful.

11
  • 8
    View the source of your web page. I bet you'll see the other part of your script's code in there. Commented Sep 4, 2013 at 19:12
  • 2
    I'm not sure if it matters, but shouldn't <?PHP be <?php (lowercase)? Commented Sep 4, 2013 at 19:14
  • 1
    Is this in a .php or a .html file? If it's not .php, this won't work. Commented Sep 4, 2013 at 19:14
  • @RocketHazmat It's a .php Commented Sep 4, 2013 at 19:15
  • 1
    @user2741021: Are you sure PHP is running at all? Try just <?php phpinfo(); ?>. Commented Sep 4, 2013 at 19:18

2 Answers 2

5

PHP is not being processed at all. Ensure that your page is a .php page and on a PHP server. Otherwise, ensure that Apache is processing PHP for whatever file extension you are using.

Also, note that it is more common to use lowercase PHP in <?php instead of <?PHP, although it appears that capitalization doesn't matter.

Sign up to request clarification or add additional context in comments.

Comments

2

Because of the wrong file name OR a misspelled start of php code i.e. <?PHP instead of <?php following code is being considered part of a HTML tag:

 <?PHP
        if (isset ($_FILES["file"])) {
            $allowedExts = array("jpeg", "jpg", "png");
            $temp = explode(".", $_FILES["file"]["name"]);
            $extension = end($temp);
            if ((($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/jpg")
            || ($_FILES["file"]["type"] == "image/pjpeg")
            || ($_FILES["file"]["type"] == "image/x-png")
            || ($_FILES["file"]["type"] == "image/png"))
            && ($_FILES["file"]["size"] < 1048576)
            && in_array($extension, $allowedExts))
            {

            if ($_FILES["file"]["error"] >

Note start <.... end >

And rest is what you see in your browser verbatim.

Rename your file with .php extension.

And preferably change PHP code as:

<?php
// PHP code here
?>

EDIT: Thanks for all the comments, it appears <?PHP or <?php both will work.

EDIT 2: Another thing to check is Apache config for a line similar to this:

AddType application/x-httpd-php .php

Which basically tells Apache to treat all .php files as PHP scripts.

4 Comments

I just checked and <?PHP ... ?> works. I'm a bit surprised at that.
Capitalization does not matter for the open tag.
PHP's rather lax about caps, actually. Only variable names, array keys, and some constant names are case-sensitive.
You mean <?php not <php!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.