Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP check if file is an image [duplicate]

Is there a way to make sure a received file is an image in PHP?

Testing for the extension doesn't sound very secure to me as you could upload a script and change its extension to whatever you want.

I've tried to use getimagesize too, but there might be something more suited for that particular problem.

like image 840
sf89 Avatar asked Mar 14 '13 11:03

sf89


People also ask

How check file is image or not in php?

php /* 1 = Check if the file uploaded is actually an image no matter what extension it has 2 = The uploaded files must have a specific image extension */ $validation_type = 1; if($validation_type == 1) { $mime = array('image/gif' => 'gif', 'image/jpeg' => 'jpeg', 'image/png' => 'png', 'application/x-shockwave-flash' => ...

How can I tell if file is image or video php?

php $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension foreach (glob("*") as $filename) { echo finfo_file($finfo, $filename) . "\n"; } finfo_close($finfo); ?> Save this answer. Show activity on this post.

How to get path of uploaded file in php?

php $dir = dirname(__FILE__); echo "<p>Full path to this dir: " . $dir . "</p>"; echo "<p>Full path to a . htpasswd file in this dir: " .


1 Answers

The getimagesize() should be the most definite way of working out whether the file is an image:

if(@is_array(getimagesize($mediapath))){     $image = true; } else {     $image = false; } 

because this is a sample getimagesize() output:

Array ( [0] => 800 [1] => 450 [2] => 2 [3] => width="800" height="450" [bits] => 8 [channels] => 3 [mime] => image/jpeg) 
like image 67
George Mickleburgh Avatar answered Sep 22 '22 13:09

George Mickleburgh