You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

上传大文件时getimagesize()函数触发警告的技术问题问询

Fixing getimagesize() Warnings for Large File Upload Validation

Looks like you're running into a common snag with getimagesize() when handling large file uploads—let's break down why this happens and how to fix it cleanly.

Why the Warning Occurs

When you upload a file that exceeds PHP's configured limits (upload_max_filesize or post_max_size in php.ini), PHP rejects the upload entirely. This means $_FILES["banner_708"]["tmp_name"] might not exist at all, or points to an empty/invalid file. Calling getimagesize() on this non-existent file triggers a warning, and your validation logic breaks because it's trying to check a file that never made it to the server.

Also, your current check has a redundancy: $check !== false || !empty($check) is unnecessary because getimagesize() returns false only for invalid images, and returns a non-empty array for valid ones. That extra condition doesn't add any value.

Improved Validation Code

Here's a revised version of your code that handles large files properly and avoids warnings:

// Initialize upload flag and error message array
$uploadOK = 1;
$banner_msg_array = [];

// First, check if the upload encountered an error (like oversized file)
if ($_FILES["banner_708"]["error"] !== UPLOAD_ERR_OK) {
    $uploadOK = 0;
    switch ($_FILES["banner_708"]["error"]) {
        case UPLOAD_ERR_INI_SIZE:
        case UPLOAD_ERR_FORM_SIZE:
            array_push($banner_msg_array, "The file is too large.");
            break;
        default:
            array_push($banner_msg_array, "File upload failed with error code: " . $_FILES["banner_708"]["error"]);
    }
}

// Only validate image if upload was successful
if ($uploadOK) {
    // Ensure the temporary file exists before checking its type
    if (file_exists($_FILES["banner_708"]["tmp_name"]) && is_file($_FILES["banner_708"]["tmp_name"])) {
        $check = getimagesize($_FILES["banner_708"]["tmp_name"]);
        if ($check !== false) {
            $uploadOK = 1;
        } else {
            $uploadOK = 0;
            array_push($banner_msg_array, "This is not a valid image.");
        }
    } else {
        $uploadOK = 0;
        array_push($banner_msg_array, "Uploaded file is missing or invalid.");
    }
}

Key Fixes & Improvements:

  • Check upload errors first: We use $_FILES["banner_708"]["error"] to catch issues like oversized files before attempting any image validation. This eliminates the getimagesize() warning entirely.
  • Validate temporary file existence: We add a check to confirm the temp file exists and is a valid file before calling getimagesize().
  • Simplified validity check: Removed the redundant || !empty($check) condition since getimagesize()'s return value is sufficient to determine validity.

Bonus Tips:

  • Add a max_file_size attribute to your HTML file input to give users a client-side hint about allowed file sizes before they upload.
  • Double-check your php.ini settings for upload_max_filesize, post_max_size, and memory_limit to ensure they align with your application's needs.

内容的提问来源于stack exchange,提问作者user9041892

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:16:41