0

I would like to allow users to be able to upload images to my website although i am having trouble implementing this feature.

<?php
     if (isset($_POST['uploadImg'])) {
    $image = $_FILES['image'];

    $imageName = $_FILES['image']['name'];
    $imageTmpName = $_FILES['image']['tmp_name'];
    $imageSize = $_FILES['image']['size'];
    $imageError = $_FILES['image']['error'];
    $imageType = $_FILES['image']['type'];

    $getImageExt = explode('.', $imageName);
    $imageExt = strtolower(end($getImageExt));

    $allowed = array('jpg', 'jpeg', 'png', 'tiff');

    if (in_array($imageExt, $allowed)) {
      if ($imageError === 0)  {
          if ($imageSize < 8000) {
              $imageDestination = 'images/'.$imageName;
              move_uploaded_file($imageTmpName, $imageDestination);
              echo"uploaded successfully";
          } else {
              echo"File is to big";
          }
      } else {
          echo "Error uploading file";
      }
    } else {
        echo"Invalid file format";
    }

}
?>

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype"multipart/form-data">
    <input type="file" name="image">
    <button type="submit" name="uploadImg">Upload</button>
</form>

This is the code I currently have, which is failing at the if statement which checks the image type. I have tried uploading all allowed image types but none are working

I tried implementing the script found at w3schools but this wasn't working for me. I also tried to use the fixes found here How to get the file extension in PHP? but i still couldn't get a solution.

Any help would be appreciated.

Also, is it possible to do this asynchronously?

6
  • script is working fine. make sure you add enctype in from tag and also increase the size and check. you have permission to add image to image folder Commented Apr 7, 2018 at 17:42
  • i have the correct permissions for the folder, i have update my original post with my form code. I will change the file size thanks. It is still failing at the file type check though Commented Apr 7, 2018 at 18:03
  • 1
    var_dump($_FILES); Commented Apr 7, 2018 at 18:09
  • Take a look at what's in there (see the comment right above), it's not giving you the extension name, it's giving you the mime-type from the browser, which will look like image/jpeg instead. Commented Apr 7, 2018 at 18:15
  • You can, however, use pathinfo() to get the extension name instead. If you do that, normalize both to lower or uppercase. Commented Apr 7, 2018 at 18:17

1 Answer 1

1

Problem is in this line. change this line

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype"multipart/form-data">

To

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype="multipart/form-data">

You have missed '=' in enctype attribute

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

Comments

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.