0

there is a little form on one page which has the code below,

<div class="postcomment">
<form id="comments" action="insertcomment.php" method="POST" enctype="multipart/form-data">
Comment: <input type="text" name="comment" id="commentfield">
<input type="submit" name="submit" value="Post comment" class="button">
<br>
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
Image: <input type="file" name="image" />
<br>
</div>

once the user adds the picture by browsing for it the form then goes to the insertcomment.php code which is below

$target_path = "images/";

$file_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $file_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

and for some reason it is displaying the error and not showing in the images directory, the error is: Parse error: syntax error, unexpected T_IF in /homepages/21/d417005970/htdocs/rk8479/htdocs/insertcomment.php on line 18

4
  • You have a syntax error in your code, like a missing ; or unclosed }, but it isn't in the code you posted. What is line 18 (and the surrounding lines) of insertcomment.php? Commented Nov 24, 2012 at 1:46
  • @MichaelBerkowski its the if statement because there is code above the file upload one but i removed it when i posted it on this site because that code was separate and was working fine with and without the file upload part Commented Nov 24, 2012 at 1:49
  • But what I'm saying is there is no syntax error in this code. There is more code, and the error occurs elsewhere, even if it is first reported at the T_IF. Commented Nov 24, 2012 at 1:52
  • you are also not calling the correct variable, your file is named "image" and you are calling "uploadedfile" Commented Nov 24, 2012 at 1:52

1 Answer 1

1

Try this HTML

<div class="postcomment">
<form id="comments" action="insertcomment.php" method="POST" enctype="multipart/form-data">
Comment: <input type="text" name="comment" id="commentfield">
<br>
Image: <input type="file" name="image" />
<br>
<input type="submit" name="submit" value="Post comment" class="button">
</form>
</div>

PHP CODE (you are trying to use it on same page you can check for if(isset($_POST['submit']))

if($_FILES['image']['size'] > 0){
        $allowedExts = array("jpg", "jpeg", "gif", "png");
        $extension = end(explode(".", $_FILES["image"]["name"]));
        if ((($_FILES["image"]["type"] == "image/gif")
        || ($_FILES["image"]["type"] == "image/jpeg")
        || ($_FILES["new_image"]["type"] == "image/png")
        || ($_FILES["image"]["type"] == "image/pjpeg"))
        && ($_FILES["image"]["size"] < 1048576)
        && in_array($extension, $allowedExts))
          {
          if ($_FILES["image"]["error"] > 0)
            {
            $error_message = $_FILES["image"]["error"];
            }
          else
            {

            if (file_exists("images/" . $_FILES["image"]["name"]))
              {
              $error_message = $_FILES["image"]["name"] . " " . $LANG['image_exist'];
              }
            else
              {
              if(move_uploaded_file($_FILES["image"]["tmp_name"],
              "images/" . $_FILES["image"]["name"])) {  
              // success
              $image_name = $_FILES["image"]["name"];
              } else {
              $error_message = "Upload Failed!";
              }
              }
            }
          }
        else
          {
          $error_message = "Error: May be different ext or size";
          }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

i had to take out the mysql_prep because it caused a fatal error saying i called an undefine function but once i took it out it all worked, thankyou :)
sorry i just paste my code i always use mysql_prep :P I have removed it Thanks for pointing it

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.