Php dosya yüklemeden önce resim dosyası türünü ve boyutunu kontrol edin

7 Cevap

Ben aşağıdaki kodu vardır:

$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();
}

Ne yapmam gerekiyor yüklenen dosya jpg / gif / png ve boyut olarak daha da az 2 MB'lik eğer kontrol etmektir.

Onun daha büyük 2 megabite, veya doğru dosya türü, i / echo F2 (API için hata kodu) dönmek gerekir.

I 70k jpg dosyayı işlemek için yukarıdaki kodu kullandığınızda, F2 döndürür.

SUBNOTE the picture im uploading has an extension of .JPG. Could case be a factor? If so, how do i accommodate for that?

7 Cevap

Eğer dosya türünü belirlemek için dosya uzantıları güvenmek istemeyebilirsiniz unutmayın. Birisi örneğin bir .png uzantılı bir yürütülebilir dosya yüklemek için oldukça kolay olacaktır. Bir mim-türü de kolaylıkla bir görüntü olarak geçirmek için kötü niyetli bir müşteri tarafından taklit edilebilir. Relying on that information is a security risk.

PHP Documentation:
The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

gd (getimagesize() ) to make sure they are actually valid images (and not just random files pretended with the header of an image file... finfo_file bu başlıklarla dayanan) ile görüntüleri yüklemeyi deneyin.

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();
    }
}


Eğer gerçekten dosya bir görüntü ise küçük harflerle içine uzantısı koymak için strtolower() kullanmak, doğrulamak için uzantısı kullanmak gerekir.

$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();
}

SUBNOTE resim im yükleme. JPG bir uzantısı vardır. Durumda bir faktör olabilir mi? Eğer öyleyse, nasıl ben bunun için karşılamak mı?

Evet, bu bir sorundur. Bu çizgiye strtolower () eklemeniz gerekir:

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

gibi:

$ Ext = strtolower (substr ($ FileCheck, strrpos ($ FileCheck) + 1) '.');

Bu sizin şu anda sorunu çözecektir. Ama teknik dosya uzantıları hakkında endişe etmemesi gerektiğini, gerçekten sadece MIME türünü kontrol etmek gerekir gerekir

Gibi karşılaştırmalar $ext == "jpg" sadece $ext tam olarak "jpg" olduğunu kontrol edin.

Sen ". JPG" durumla başa çıkmak için, bu karşılaştırmalar yapmadan önce $ ext strtolower kullanmak isteyebilirsiniz.


If you are using PHP <= 5.2, you might want to use mime_content_type to check the content-type of the files, instead of relying on $_FILES['imagefile']['name'] and/or $_FILES["imagefile"]["type"], which are both sent by the client -- and can, as such, be faked.

PHP> = 5.3 kullanıyorsanız, size yeni uzantı fileinfo düşünebilirsiniz, ve finfo_file bir fonksiyon


For the size of the file, you are already using $_FILES["imagefile"]["size"] ; that's OK, I guess, but you will only know it when the file has been uploaded -- still, there is no real way of checking that kind of thing before upload, I'm afraid...


you might be able to find some JS code to do a first pre-check of extension before the file is uploaded -- but you'll still have to check on the server side, as anything done client-side is inherently not secure.

Sen değil ama, dosyanın boyutu hakkında aynı şeyi olabilir emin ...

Bazı tarayıcılar MAX_FILE_SIZE (file upload ile ilgili belgelere bakın) adında gizli bir alanda destek olabilir; ama gerçekten desteklendiğinden emin değil (aslında, kullanılan görmedim; yani muhtemelen :-( değil)


As a sidenote, you will probably want to configure upload_max_filesize, so it allows upload at least as big as what you want (by default, it is generally set to 2MB ; so should already be OK for you)

Dosya boyutu oldukça açıktır, ama ne insanlar doğru biçimi olduğunu kontrol etmek için yukarıda yapıyoruz biraz verimsiz ve "güvensiz" olduğunu.

İşte ne var:

if($_FILES["userPicture"]["error"] == 0) {
// File was uploaded ok, so it's 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;
    }
}

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

Edit: gerçekten bir hata yoktu söyler dosyalar dizi değeri "hata" hariç, dosya ya da verilen şey kullanıcı güveniyor değil çünkü bu "her tarayıcıda" çalışma avantajına sahiptir.

take note that for Internet Explorer, they submit JPEG file's mime type as image/pjpeg - which is different from other browsers.

Burada dosya uzantısı karşı mim türü almak için basit bir fonksiyon bulunuyor:

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 '';
}

Php bir sunucu tarafı betik dili ve resim istemci tarafında iseniz, before karşıya php aracılığıyla boyutunu kontrol edebilirsiniz.

Iki şey gerekir:

Lütfen uzantısı onay harf duyarsız olun:

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

Dosya aslında bir görüntü içeriyorsa edin. Bunu yapmanın kolay bir yolu olduğunu fileinfo:

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

Bir dosya gerçekten bir görüntü olup olmadığını kontrol için bir alternatif yol gd gibi bir görüntü kütüphanede yükleniyor. Dosya başarıyla yüklendi edilebilir ise, bir görüntü.

Jpeg görüntüleri ya .jpg veya .jpeg uzantısı olabilir unutmayın.