Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I just wanna check if this is the best method to validate a image before uploading. Please see below my code and advise?

Many thanks for reading my post.

What I have tried:

<?php
if(isset($_POST['save'])) {

//Validate image
if (empty($_POST["image"])) {
$imageError = "";
} else {
$image = check_input($_POST["image"]);
$allowed =  array('jpeg','jpg', "png", "gif", "bmp", "JPEG","JPG", "PNG", "GIF", "BMP");
$ext = pathinfo($image, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
$imageError = "jpeg only";
}
}

}

// Validate data
function check_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
<?
Posted
Updated 18-Oct-22 2:56am

The code might also need to check for content type instead just the file extension.

MIME type detection for PHP file uploads | finalwebsites.com[^]

PHP: mime_content_type - Manual[^]
 
Share this answer
 
v2
Comments
Member 14093672 12-Jan-19 23:26pm    
or this one is better?

<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size = $_FILES['image']['size'];
      $file_tmp = $_FILES['image']['tmp_name'];
      $file_type = $_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
      $expensions= array("jpeg","jpg","png");
      
      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
      
      if($file_size > 2097152) {
         $errors[]='File size must be excately 2 MB';
      }
      
      if(empty($errors)==true) {
         move_uploaded_file($file_tmp,"images/".$file_name);
         echo "Success";
      }else{
         print_r($errors);
      }
   }
?>
<html>
   <body>
      
      <form action = "" method = "POST" enctype = "multipart/form-data">
         <input type = "file" name = "image" />
         <input type = "submit"/>
			
         <ul>
            <li>Sent file: <?php echo $_FILES['image']['name'];  ?>
            <li>File size: <?php echo $_FILES['image']['size'];  ?>
            <li>File type: <?php echo $_FILES['image']['type'] ?>
         </ul>
			
      </form>
      
   </body>
</html>
Bryian Tan 12-Jan-19 23:48pm    
check this out: MIME type detection for PHP file uploads | finalwebsites.com[^], the mime_content_type() depreciated, my bad.
Member 14093672 13-Jan-19 0:51am    
I am struggling to append this to the existing function above.

function get_mime_type($file) {
 $mtype = false;
 if (function_exists('finfo_open')) {
 $finfo = finfo_open(FILEINFO_MIME_TYPE);
 $mtype = finfo_file($finfo, $file);
 finfo_close($finfo);
 } elseif (function_exists('mime_content_type')) {
 $mtype = mime_content_type($file);
 } 
 return $mtype;
}
if(empty($_FILES["img"])){
$imgErr="";
$flag=false;
}else{
// step given the name of
$filename=$_FILES['img']['name'];
//allowed image extension
$extension=array('jpeg','jpg','png','gif','bmp','JPEG','JPG','PNG','GIF','BMP');
//image type
$filetype=$_FILES['img']['type'];
//tempary name
$file_tmp=$_FILES['img']['tmp_name'];
//how this location to upload
move_uploaded_file($file_tmp,"images/".$filename);
$ext=pathinfo($filename,PATHINFO_EXTENSION);
//check the picture
if(!in_array($ext,$extension)){
$imgErr="jpeg, png,gif,bmp,jpeg only!";
}

}
 
Share this answer
 
Comments
Richard Deeming 18-Oct-22 9:08am    
Unformatted, unexplained code with a serious security vulnerability is not a good solution to this question.

Stick to answering new questions unless you have something new and interesting to add to the discussion.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900