title: PHP check if uploaded file is an image
body: <p>I'm trying to create user avatars. I'm just trying to make sure its secure. What is the best way to check if the file is an actual image, and not anything else.</p>
  
  <p>I've tried this</p>
  
  <pre><code>$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
      if($check !== false) {
          echo "File is an image - " . $check["mime"] . ".";
  
      } else {
          echo "File is not an image.";
      }
  </code></pre>
  
  <p>Although this seems to work on some images, but other images like photos seem to make it fail. Photos that i've taken with my phone seem to make it appear with "File is not an image" while others make it appear with an image. </p>
  
  <p>I've also been checking the file formats</p>
  
  <pre><code>if($imageFileType != "jpg" &amp;&amp; $imageFileType != "png" &amp;&amp; $imageFileType != "jpeg"
  &amp;&amp; $imageFileType != "gif" ) {
      echo "Sorry, only JPG, JPEG, PNG &amp; GIF files are allowed.";
  
  }
  </code></pre>