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?

Sabathaon
  • 45
  • 8
  • 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 – prasanna puttaswamy Apr 07 '18 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 – Sabathaon Apr 07 '18 at 18:03
  • 1
    `var_dump($_FILES);` – CBroe Apr 07 '18 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](http://php.net/manual/en/features.file-upload.post-method.php), which will look like `image/jpeg` instead. – Jared Farrish Apr 07 '18 at 18:15
  • You can, however, use [`pathinfo()`](http://php.net/manual/en/function.pathinfo.php) to get the extension name instead. If you do that, normalize both to lower or uppercase. – Jared Farrish Apr 07 '18 at 18:17
  • @JaredFarrish thanks I will try with these and see if I can get it working – Sabathaon Apr 07 '18 at 18:32

1 Answers1

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