Search code examples
file-uploadcodeigniter-4

I need help when I try to upload an image CodeIgnite4


I need help when I try to upload an image

I get this error and I don't understand why avatar is not a valid uploaded file.

my code in controller

rules:

'avatar' => [
                'rules' => 'required|uploaded[avatar]|max_size[avatar,1024]|ext_in[avatar,jpg,jpeg,png]',

                'errors' => [
                    'required' => lang('Core.Auth.Error.required'),
                    ]
            ],

upload:

if (!$this->validate($rules)) {
            $data['validation'] = $this->validator;
        } else {

                $avatar = $this->request->getFile('avatar');
                   $newName = $avatar->getRandomName(); 
                   $avatar->move(WRITEPATH.'uploads', $newName);
                   $filepath = base_url()."/selfie/".$newName;

my php.ini

upload_max_filesize = 1G

post_max_size = 1G

memory_limit = 1G


Solution

  • From CodeIgniter documentation:

    The uploaded rule fails if the name of the parameter does not match the name of any uploaded files.

    The exact error message you posted will appear if this rule cannot be satisfied

    Double check your HTML <input> tag and make sure that both name attribute of this tag and the name passed to uploaded rule are the same, e.g.:

    // In the HTML
    <input type="file" name="avatar">
    
    // In the controller
    $this->validate([
        'avatar' => 'required|uploaded[avatar]|max_size[avatar,1024]|ext_in[avatar,jpg,jpeg,png]'
    ]);