Reputation: 11
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.
Upvotes: 0
Views: 106
Reputation: 786339
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.
Upvotes: 2
Reputation: 17487
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.
Upvotes: 5