3

I have a problem uploading a file in my form using CodeIgniter 4 and I can't find what's wrong. Can you help me, please? The name of the file is ok, the problem is that it doesn't move the file in the specified directory because I get this error: Call to a member function move() on null. WRITEPATH has the value: C:\xampp\htdocs\myproject\writable\

The part of the view where I upload looks like this :

<div class="card-body">
    <label for="exampleInputFile">Fisier</label>
    <div class="form-group">
        <div class="input-group">
            <div class="custom-file">
                <input type="file" class="custom-file-input" name="customFile" id="customFile" required>
                <label class="custom-file-label" for="exampleInputFile">Alege Fisierul</label>
            </div>
        </div>
    </div>
</div>

And the part of the controller function where I try to move the file in a specific folder looks like this :

$img = $this->request->getFile('customFile');
$img->move(WRITEPATH . 'uploads');

I take the name of the file with this : $this->request->getVar('customFile')

Thank you !

2
  • Does this answer your question? Call to a member function move() on null Commented Oct 15, 2020 at 12:29
  • Can you show your entire form please? Can you also post the output of $_FILES? Maybe you forgot yo put your form as multipart. Commented Oct 15, 2020 at 13:35

2 Answers 2

5

This worked in Codeigniter 4:

 $file = $this->request->getFile('file');

        if (!$file->isValid()) {
            return $this->fail($file->getErrorString());
        }

        $file->move(ROOTPATH . 'public\uploads\documents');

        $name = $file->getName();

Thank you!

Sign up to request clarification or add additional context in comments.

Comments

0

In Controller Like this...

$config['upload_path'] = './upload/painting/';
    $config['allowed_types'] = 'jpg|png|jpeg'; // here you want to define file type
    $config['max_width'] = '250';
    $config['max_height'] = '300';
    $config['encrypt_name'] = TRUE;
    $this->load->library('upload', $config);
    //  $this->upload->do_upload('profile_pic');
        if ($this->upload->do_upload('ptn_url')) {
          $data = array('upload_data' => $this->upload->data());
    //    print_r($data);
          $_POST['ptn_url'] = "upload/painting/" . $data['upload_data']['file_name'];
          $_POST['user_id'] = $this->session->user_data['user_id'];

in routes.php file which is located at config folder you need to define path for store image like this...

$route['upload-image'] = 'user';
$route['store-image'] = 'user/add';
$route['painting/(:num)'] = 'painting';

for More Reference about File Upload Visit CodeIgniter 3 File Upload

I hope this is helps to you...

Thank You...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.