Tue, January 25, 2022
InterServer Web Hosting and VPS
https://devcodepro.comdevcodepro
X
InterServer Web Hosting and VPS
Home · Latest · Trends
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 } ?>
rated 20 times (19) (1)
comments: 2 / hits: 10770  / 5 years ago, mon, nov 21, 16, 10:48:23
More From » PHP
 

Comments

3
Caution
This function expects filename to be a valid image file. If a non-image file is supplied, it may be incorrectly detected as an image and the function will return successfully, but the array may contain nonsensical values.

Do not use getimagesize() to check that a given file is a valid image. Use a purpose-built solution such as the Fileinfo extension instead.


https://www.php.net/manual/en/function.getimagesize.php
[#28] Friday, February 14, 2020, 4:04:08
 
Wolterhon
commented 2 years ago
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
 
suppe
commented 2 years ago
Only authorized users can post. Please sign in first, or register a free account
Login with Google
 
Share
Posted
Ronald
Member since Sep 6, 2016
Total Code Snippets: 25
Total Comments: 2
Location: New Britain, Connecticut
Ronald snippets
6 years ago, tue, sep 6, 16, 10:11:38
//Update $sql = $DB->Prepare('UPDATE mytable SET count = count + ? WHERE id = ?'); if($DB->Execute($sql,array("1",$id)) === false) { print $DB->ErrorMsg(); } //Insert $sql = $DB->Prepare('INSERT INTO mytable (id, mytext) VALUES (?, ?)'); if($DB->Execute($sql,array($id,$text)) === false) { print $DB->ErrorMsg(); }
comments: 0 / hits: 2743
PHP
6 years ago, tue, sep 6, 16, 10:18:23
//MySQL CREATE TABLE adodb_logsql ( created datetime NOT NULL, sql0 varchar(250) NOT NULL, sql1 text NOT NULL, params text NOT NULL, tracer text NOT NULL, timer decimal(16,6) NOT NULL ); //PHP include ('classes/adodb/db.php'); include ('classes/adodb/adodb.inc.php'); $adoDriver = "mysqli"; $DB = ADONewConnection($adoDriver); @$DB->Connect($server,$user,$password,$database); $perf = NewPerfMonitor($DB); $perf->UI($pollsecs=3);
comments: 0 / hits: 2593
PHP
6 years ago, tue, sep 6, 16, 10:42:24
<?php $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $to = '[email protected]'; $subject = 'Email Subject'; $text = 'Test email body'; if (mail($to, $subject, $text, $headers)) { echo "Test php mail sent successfully"; } else { echo"Message delivery failed"; } ?>
comments: 0 / hits: 2974
PHP
5 years ago, fri, sep 16, 16, 1:10:12
{if {$date1|date_format:"%y%m%d"} lt {$date2|date_format:"%y%m%d"}} foo {/if} or {if {$date|date_format:"%y%m%d"} lt {$smarty.now|date_format:"%y%m%d"}} foo {/if}
comments: 0 / hits: 3475
PHP
5 years ago, fri, sep 16, 16, 1:12:32
input:-webkit-autofill{-webkit-box-shadow:0 0 0px 50px #F5F5F5 inset;opacity:0.6;}
comments: 0 / hits: 2235
CSS
5 years ago, fri, sep 16, 16, 1:27:33
$domain = 'dailymail.co.uk'; $dots = substr_count($domain, '.'); if($dots == 1){ $results = preg_split('/(?=.[^.]+$)/', $domain); echo $results[0]; } if($dots == 2){ $results = preg_split('/(?<=[^0-9])[.](?<![0-9])/', $domain); echo $results[0]; }
comments: 0 / hits: 3387
PHP
5 years ago, sun, sep 25, 16, 2:52:42
<!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> </head> <body> <input type="text" id="number" name="phonenumber" /> <script> jQuery('#number').keyup(function () { this.value = this.value.replace(/[^0-9.s+()]/g,''); }); </script> </body> </html>
comments: 1 / hits: 2729
5 years ago, thu, nov 10, 16, 4:21:59
//libs/plugins/modifier.removebb.php <?php /** * Smarty plugin * * @package Smarty * @subpackage plugins */ /** * smarty_modifier_removebb() * * @param mixed $string * @return */ function smarty_modifier_removebb($string) { $find = '|[[/!]*?[^[]]*?]|si'; $replace = ''; return preg_replace($find,$replace,$string); } ?> // Usage: {$m.foo|removebb}
comments: 1 / hits: 2867
PHP
5 years ago, sun, dec 25, 16, 2:44:24
<div class="ckarea" style="display:none;">Please enter Description</div> <textarea name="longdesc" id="longdesc"></textarea> <script> CKEDITOR.replace( 'longdesc' ); </script> <script> $("form").submit(function(e) { var description = CKEDITOR.instances['longdesc'].getData().replace(/<[^>]*>/gi, '').length; if (!description) { $(".ckarea").show(); return false; } }); </script>
comments: 0 / hits: 5577