简体   繁体   中英

PHP file upload script not uploading

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

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";
          }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM