codeigniter 4 中的图像验证大小设置

问题描述 投票:0回答:1

如何在 ci4 应用程序中准确指定要上传的图像的大小。我想上传 49.7kb 的图像,但由于大小限制而无法上传。 如何将大小设置为最大 5mb?目前我想我将其设置为 5mb 我的形象规则是:

$profile_pix_validation_rule = [
                'userfile' => [
                    'label' => 'Image File',
                    'errors' => ['max_size' => "Image size can be 10mb max"],
                    'rules' => [
                        'uploaded[passport_image]',
                        'mime_in[passport_image,image/jpg,image/jpeg,image/png]',
                        'max_size[profile_pix,5120]',
                        'max_dims[profile_pix,1024,768]',
                    ],
                ],
    ];
php codeigniter codeigniter-4
1个回答
0
投票

也许这是因为您的字段名称不同。

$profile_pix_validation_rule = [
    'passport_image' => [ // The correct field name
        'label' => 'Image File',
        'rules' => [
            'uploaded[passport_image]', // The field name here should match the input name
            'mime_in[passport_image,image/jpg,image/jpeg,image/png]', // Allowed mime types
            'max_size[passport_image,5120]',
            'max_dims[passport_image,1024,768]',
        ],
        'errors' => [
            'max_size' => 'Image size can be 5 MB max',
            'uploaded' => 'Please upload an image file.',
            'mime_in' => 'Only JPG, JPEG, and PNG files are allowed.',
            'max_dims' => 'Image dimensions should not exceed 1024x768 pixels.',
        ]
    ],
];

此外,表单应使用

enctype="multipart/form-data"

创建

表单示例(基本html)

<form method="post" enctype="multipart/form-data">
    <input type="file" name="passport_image">
    <input type="submit" value="Upload">
</form>

并且在

php.ini

upload_max_filesize = 5M
post_max_size = 8M
max_execution_time = 300
max_input_time = 300
© www.soinside.com 2019 - 2024. All rights reserved.