简体   繁体   English

Codeigniter:无法上传图片(你没有select文件上传)

[英]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.)数组([错误] => 你没有 select 文件上传。)

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我的 Controller

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

My Model我的 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 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.您必须在表单标签中添加enctype="multipart/form-data"才能通过 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我的第一个问题是我必须添加到我的表单标签: enctype="multipart/form-data"正如其他人已经在评论中指出的那样

My second problem was that I had to add in my autoload.php file $autoload['libraries'] = array('database',.., 'upload');我的第二个问题是我必须添加我的 autoload.php 文件$autoload['libraries'] = array('database',.., 'upload');

Last but not least I changed $this->load->library('upload', $config);最后但同样重要的是,我更改$this->load->library('upload', $config); to $this->upload->initialize($config);$this->upload->initialize($config); Somehow the configuration was not able to initialize correctly, so this fixed my last problem.不知何故,配置无法正确初始化,所以这解决了我的最后一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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