Dosya uzantıları, en azından yapabilirsiniz yalan, ya da. Bu / güven kullanıcı verilerini teslim güvenmemek kesinlikle en iyisidir. Ayrıca, PHP kılavuzu notları gibi:
$_FILES['userfile']['type'] -
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.
Bu güvenilir değildir.
Burada daha iyi bir yolu var:
PHP'nin getimagesize()
function returns a numerically indexed array of data about the image, and index 2 is one of the IMAGETYPE_XXX constants (a full list of which is available here) görüntü türünü göstermektedir. Bunlar daha sonra GD etti fonksiyonları bir dizi kullanılabilir, iki ilgili olanlar image_type_to_extension()
and image_type_to_mime_type()
a> olmak.
Yani, kolayca bu satırlar boyunca bir şey yapabilirsiniz:
$imageData = getimagesize($_FILES['userfile']['tmp_name']);
// $imageData[2] will contain the value of one of the constants
$mimeType = image_type_to_mime_type($imageData[2]);
$extension = image_type_to_extension($imageData[2]);
Eğer exif
uzantısı mevcut olan IIF, [exif_imagetype()][5]
function getimagesize()
indeksi 2 olarak aynı sonucu dönmek ama çok daha hızlı olacak, rağmen.
Daha yaygın mevcut PHP genelinde yükler çünkü ben, benim birincil örnek olarak GD yöntemleri kullandım. Ama Imagick uzatma da benzer bir işlevsellik sunuyor ve ayrıca (btw 5.3, beri dahil) fileinfo
uzantısı ile mime türünü doğrulamak olabilir.