phpcodeignitercodeigniter-4

image validation size setting in codeigniter 4


How can I specify accurately the size of image to be uploaded in a ci4 application. I wanted to upload an image of 49.7kb but cannot because of size restriction. How can I set the size to 5mb max? Currently I guess i set it to 5mb My image rules are:

$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]',
                    ],
                ],
    ];

Solution

  • Maybe this is because your field name is different.

    $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.',
            ]
        ],
    ];
    

    Additionally, the form should created with enctype="multipart/form-data"

    Example of form (basic html)

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

    And In php.ini

    upload_max_filesize = 5M
    post_max_size = 8M
    max_execution_time = 300
    max_input_time = 300