18
How to check if uploaded file is image in php
How to detect if uploaded file is image or not with php
<?php
//upload.php
if(isset($_POST['query'])) {
$image_info = @getimagesize($_FILES['image']['tmp_name']);
if($image_info == false) {
die('Please upload valid image file.');
} else {
echo 'Image file is valid';
}
} else {
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="query" value="Upload" />
</form>
<?php } ?>
Interserver | Standard & VPS Cloud Hosting | $2.50 /Month
Flexible VPS hosting platform to deploy your online projects.
Economical and balanced between processor cores, memory and storage
rated 20 times
(19)
(1)
comments: 2 / hits: 12631
/ 6 years ago, mon, nov 21, 16, 10:48:23
More From
» PHP
Comments
3
@Wolterhon true it should be something like this:
Code:
<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $_FILES['newimage']['tmp_name']);
if ($mimetype == 'image/jpg' || $mimetype == 'image/jpeg' || $mimetype == 'image/gif' || $mimetype == 'image/png') {
echo 'File type ' . $mimetype . ' is allowed';
} else {
echo 'The source file type ' . $mimetype . ' is not supported';
die();
}
?>
[#29] Sunday, February 16, 2020, 5:46:27
commented 2 years ago
https://www.php.net/manual/en/function.getimagesize.php