文件大小检查不起作用

时间:2015-08-28 07:13:00

标签: php file upload

我正在尝试上传文件。当我检查文件大小时, if file_size < limit只是工作没有任何问题,但什么时候 file_size > limit它不显示错误(这是一个回声)。

        if($_SERVER['REQUEST_METHOD']=="POST"){
        if(isset($_POST['order']) && !empty($_POST['order'])){
            if(is_uploaded_file($_FILES['file']['tmp_name'])){
                $upload_types= array('application/x-rar-compressed','application/octet-stream','application/zip','application/octet-stream');
                if (!in_array($_FILES['file']['type'], $upload_types)){
                    echo "file type error";
                }
                elseif($_FILES['file']['size'] > 10000000){
                    echo "file size error";
                }
                else{
                    $id=mysql_real_escape_string($_POST['order']);
                    $query=mysql_query("SELECT `shop-id` FROM `orders` WHERE `id`='{$id}'");
                    $query=mysql_fetch_array($query);
                    $query=mysql_query("SELECT `id` FROM `shops` WHERE `id`='{$query['shop-id']}' AND `uid`='{$_SESSION['uid']}'");
                    if(mysql_num_rows($query)>0){
                        move_uploaded_file($_FILES['file']['tmp_name'],'upload/'.$id.get_ext($_FILES['file']['type']));
                    }
                    else{
                        HEADER("Location:user.php?error=1");
                    };
                };
            }
            else{
                HEADER("Location:user.php?error=2");
            };
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试类似:

if(isset($_FILES['uploaded_file'])) {
    $errors     = array();
    $maxsize    = 2097152;
    $acceptable = array(
        'application/pdf',
        'image/jpeg',
        'image/jpg',
        'image/gif',
        'image/png'
    );

    if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {
        $errors[] = 'File too large. File must be less than 2 megabytes.';
    }

    if(!in_array($_FILES['uploaded_file']['type'], $acceptable)) && (!empty($_FILES["uploaded_file"]["type"]))) {
        $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
    }

    if(count($errors) === 0) {
        move_uploaded_file($_FILES['uploaded_file']['tmpname'], '/store/to/location.file');
    } else {
        foreach($errors as $error) {
            echo '<script>alert("'.$error.'");</script>';
        }

        die(); //Ensure no more processing is done
    }
}