Check image file type and size before uploading file in php

I have the following code:

$filecheck = basename($_FILES['imagefile']['name']);
  $ext = substr($filecheck, strrpos($filecheck, '.') + 1);
  if (($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["imagefile"]["type"] == "image/jpeg" || $_FILES["imagefile"]["type"] == "image/gif" || $_FILES["imagefile"]["type"] == "image/png") && 
    ($_FILES["imagefile"]["size"] < 2120000)){
} else {
echo "F2";
die();
}

What I need to do is check if the downloaded jpg / gif / png file is less than 2 megabytes in size.

If it is more than 2 megabytes or the wrong file type, I need to return / echo F2 (error code for api).

When I use the above code to process the jpg 70k file, it returns F2.

SUBNOTE image im uploading has the extension .JPG. Could it be a factor? If so, how can I adapt to this?

+5
source share
7 answers

, , . - .png, . . , .

PHP:
mime , . "image/gif". mime, , PHP, .

gd (getimagesize()), , ( , ... finfo_file ).

if($_FILES["imagefile"]["size"] >= 2120000) {
  echo "F2";
  die();
} else {
    $imageData = @getimagesize($_FILES["imagefile"]["tmp_name"]);

    if($imageData === FALSE || !($imageData[2] == IMAGETYPE_GIF || $imageData[2] == IMAGETYPE_JPEG || $imageData[2] == IMAGETYPE_PNG)) {
      echo "F2";
      die();
    }
}

, , , strtolower(), .

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

if (!(($ext == "jpg" || $ext == "gif" || $ext == "png") && ($_FILES["imagefile"]["type"] == "image/jpeg" || $_FILES["imagefile"]["type"] == "image/gif" || $_FILES["imagefile"]["type"] == "image/png") && 
    ($_FILES["imagefile"]["size"] < 2120000))){
    echo "F2";
    die();
}
+22

SUBNOTE im uploading .JPG. ? , ?

, . strtolower() :

$ext = substr($filecheck, strrpos($filecheck, '.') + 1);

:

$ext = strtolower (substr ($ filecheck, strrpos ($ filecheck, '.') + 1));

. , MIME

+5

, $ext == "jpg", , $ext - "jpg".

strtolower $ext , ".JPG".


PHP <= 5.2, mime_content_type , $_FILES['imagefile']['name'] / $_FILES["imagefile"]["type"], - , , .

PHP >= 5.3, fileinfo, finfo_file


$_FILES["imagefile"]["size"]; , , , . , , , ...


- JS- , , , , , .

, , ...

MAX_FILE_SIZE (. ); , ( , , , , :-()


, , upload_max_filesize, , ( 2 , )

+5

, Internet Explorer, mime JPEG image/pjpeg - .

mime :

function mime2ext($mime){ // mime is like image/jpeg
    $m = explode('/',$mime);
    $mime = $m[count($m)-1]; // get the second part of the mime type
    switch($mime){
        case 'jpg':
        case 'jpeg':
        case 'pjpeg':
            return 'jpg';
            break;
        case 'png':
            return 'png';
            break;
        case 'gif':
            return 'gif';
            break;
    }
    return '';
}
+3

, , , , , "".

:

if($_FILES["userPicture"]["error"] == 0) {
// File was uploaded ok, so it ok to proceed with the filetype check.
    $uploaded_type = exif_imagetype($_FILES["userPicture"]["tmp_name"]);
    // What we have now is a number representing our file type.

    switch($uploaded_type) {
        case "1":
            $uploaded_type = "gif";
        break;
        case "2":
            $uploaded_type = "jpg";
        break;
        case "3":
            $uploaded_type = "png";
        break;
    }
}

;
http://www.php.net/manual/en/function.exif-imagetype.php

: " ", - , "", , .

+3

Php - , , php .

+2

:

:

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

, . - fileinfo:

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

An alternative way to check if a file is actually being uploaded to an image library, such as gd. If your file can be downloaded successfully, this is an image.

Keep in mind that jpeg images may have the extension .jpgor .jpeg.

+2
source

All Articles