PHP中的图像文件验证(大小 - 类型)

时间:2014-03-24 13:06:55

标签: php

我试图在上传之前检查图像。它的类型工作正常,但是当我尝试上传大小超过2mb的图片时,它会返回相同类型的错误..任何人都可以提供帮助!?

<?php
if ($_FILES)

{
    $imageName = date("hms").$_FILES['logo']['name'];

    if (empty($_FILES['logo']['name']))

    {
        echo "<script type='text/javascript'>alert('من فضلك اختار صورة')</script>";
    }

    elseif (!empty($_FILES['logo']['name']))
    {
        if($_FILES['logo']['type'] != 'image/jpeg'
        &&  $_FILES['logo']['type'] != 'image/JPEG'
        &&  $_FILES['logo']['type'] != 'image/jpg'
        &&  $_FILES['logo']['type'] != 'image/JPG'
        &&  $_FILES['logo']['type'] != 'image/gif'
        &&  $_FILES['logo']['type'] != 'image/GIF'
        && $_FILES['logo']['type'] != 'image/png'
        &&  $_FILES['logo']['type'] != 'image/PNG')
            {
                echo "<script type='text/javascript'>alert('يرجى إدخال صورة من امتداد jpeg, jpg, gif, png فقط')</script>";
            }

        elseif(($_FILES['logo']['size'] >2087152))
            {
                echo "<script type='text/javascript'>alert('اقصى حجم للصورة 2 ميجابايت')</script>";
            }

        else
            {
                move_uploaded_file($_FILES['logo']['tmp_name'], "styles/images/$imageName");
                mysql_query("UPDATE components SET path='$imageName' WHERE type='logo'");
                echo "<script type='text/javascript'>alert('تم رفع الصورة بنجاح')</script>";
            }
    }
}
?>

1 个答案:

答案 0 :(得分:-1)

如果是您的服务器

打开 php.ini (linux: /etc/php.ini ,windows: C:\ WINDOWS C :\ WINNT )并更改或添加以下代码:

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Maximum allowed size for uploaded files
upload_max_filesize = 4M

保存您的php.ini并重启您的服务器

OR

upload_max_filesize(仅在php.ini或.htaccess中,使用ini_set()无效)