简体   繁体   中英

Codeigniter: unable to upload image (You did not select a file to upload)

When I try to upload a image to my uploads folder i get the following error:

Array ( [error] => You did not select a file to upload.)

My View:

<form action="<?=base_url('create_public_profile_job') ?>" class="create-profile-form" method="POST">
     <input type="file" name="userfile" size="20000" />
     <button type="submit" class="create-profile-button">Submit</button>
</form>

My Controller

public function create_public_profile_job()
{ 
    if(isset($_POST['userfile']))
    {
            $this->User_model->do_upload($_POST['userfile']);
    }
}

My Model

public function do_upload($userfile)
{
    $this->load->helper('form');
    $this->load->helper('url');

    $config['upload_path']          = 'assets/uploads/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 1000000;
    $config['max_width']            = 10240000;
    $config['max_height']           = 7680000;

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload($userfile))
    {
            $error = array('error' => $this->upload->display_errors());
            print_r($userfile);
            print_r($error);
    }
    else
    {
            $data = array('upload_data' => $this->upload->data());

            print_r($data);
    }
}

The Model part is from the Codeigniter userguide https://codeigniter.com/userguide3/libraries/file_uploading.html

Don't really know where the issue is, because I pass the image through all functions

You have to add the enctype="multipart/form-data" in the form tag to upload the data through the forms.

change view to

<form action="<?=base_url('create_public_profile_job') ?>" class="create- 
profile-form" method="POST" enctype="multipart/form-data">
     <input type="file" name="userfile" size="20000" />
     <button type="submit" class="create-profile-button">Submit</button>
</form>

So I figured out what my problem was. Or better problems.

My first Problem was that I had to add to my form tag: enctype="multipart/form-data" as others already pointed out in the comments

My second problem was that I had to add in my autoload.php file $autoload['libraries'] = array('database',.., 'upload');

Last but not least I changed $this->load->library('upload', $config); to $this->upload->initialize($config); Somehow the configuration was not able to initialize correctly, so this fixed my last problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM