0

What is the quickest way on the user side to make sure an uploaded file is actually an image? I am somewhat new to php (9 months experience) and I don't know all of the functions. I am trying to build a social media site, and don't know how to figure out on the back end what file it is., but it did not seem to have a clear answer. There was another question asked that was similar to this Thank you guys in advance.

  • you have asked for both front and back end image checking, which is it? –  Jun 14 '18 at 21:30
  • Possible duplicate of [Automatic image format detection in PHP](https://stackoverflow.com/questions/189391/automatic-image-format-detection-in-php) –  Jun 14 '18 at 21:31
  • backend, when i said user side i meant what way is quickest for the user – Giovanni Kane Jun 14 '18 at 21:32
  • 1
    well the quickest way for the user is some front end validation otherwise you have to wait for the whole file to be uploaded https://stackoverflow.com/questions/3828554/how-to-allow-input-type-file-to-accept-only-image-files –  Jun 14 '18 at 21:38

1 Answers1

0

You can use php build in function: https://secure.php.net/manual/de/function.mime-content-type.php

<?php
    echo mime_content_type('php.gif') . "\n";
    echo mime_content_type('test.php');
?>

Will output:

image/gif
text/plain

Condition Example I: JPG Images only

if (\mime_content_type('image.jpg' /* full path to image */) === "image/jpg") {
    // do something
}

Condition Example II: Any type of images

if (\strpos(\mime_content_type('image.jpg' /* full path to image */), 'image/') !== false) {
    // do something
}
JamalMcCrackin
  • 620
  • 10
  • 15