如何防止更改上传的图像类型JPG为JPEG

时间:2017-12-20 08:07:47

标签: php image types

此脚本会自动将上传的图片类型JPG更改为JPEG

如何预防?我不想改变图像类型。

if (is_uploaded_file($_FILES['myfile']['tmp_name']) && ($_FILES["myfile"]["size"] <= 8192 * 1 * 8192)) {
   $enabled = array(
       IMAGETYPE_PNG,
       IMAGETYPE_JPEG
   );
   list($width, $height, $extension) = getimagesize($_FILES['myfile']['tmp_name']);
   if (in_array($extension, $enabled)) {
       $ext     = image_type_to_extension($extension, true);
       $newname = $username . $id . '-' . time() . $ext;
       $imglink = $upload_folder . '/' . $newname;
       if (@move_uploaded_file($_FILES['myfile']['tmp_name'], "$upload_folder/" . $newname))

       $result     = 1;

1 个答案:

答案 0 :(得分:0)

从文件数据中提取原始扩展名image_type_to_extension

替换此行

$ext     = image_type_to_extension($extension, true);

用这个

$ext = "." . strtolower(pathinfo($_FILES['myfile']["name"],PATHINFO_EXTENSION));

编辑:要允许JPG扩展,您必须在确认图片是getimagesize()的真实图片文件后自行检查。

$allowedExtensions = ['jpg','png','jpeg'];
if(in_array($ext, $allowedExtensions)){
    //it's an allowed extension 
}