Check picture file type and size before file upload in php

后端 未结 7 1584
走了就别回头了
走了就别回头了 2020-12-09 13:41

I have the following code:

$filecheck = basename($_FILES[\'imagefile\'][\'name\']);
  $ext = substr($filecheck, strrpos($filecheck, \'.\') + 1);
  if (($ext          


        
7条回答
  •  感动是毒
    2020-12-09 14:29

    You need two things:

    Make your extension check case insensitive:

    if($strtolower($ext == "jpg")){
      // do your stuff
    }
    

    Check if the file actually contains an image. An easy way to do this is fileinfo:

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    echo finfo_file($finfo, $filename);
    $finfo_close($finfo);
    

    An alternative way of checking if a file is really an image is loading in an image library like gd. If your file can successfully be loaded, it is an image.

    Keep in mind that jpeg images can have either the .jpg or the .jpeg extension.

提交回复
热议问题