【问题标题】:Codeigniter: unable to upload image (You did not select a file to upload)Codeigniter:无法上传图片(您没有选择要上传的文件)
【发布时间】:2020-09-19 14:49:04
【问题描述】:

当我尝试将图像上传到我的上传文件夹时,我收到以下错误:

数组([错误] => 您没有选择要上传的文件。)

我的观点:

<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>

我的控制器

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

我的模特

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);
    }
}

模型部分来自 Codeigniter 用户指南https://codeigniter.com/userguide3/libraries/file_uploading.html

真的不知道问题出在哪里,因为我通过所有函数传递图像

【问题讨论】:

    标签: php codeigniter file-upload codeigniter-3 image-upload


    【解决方案1】:

    改变视图

    <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>
    

    【讨论】:

    • 从技术上讲它是有效的。图像现在通过我的表单发送,但我仍然无法上传我的图像。我改变了我的控制器,所以现在它是:if(isset($_FILES['userfile'])) 而不是 $_POST。但现在我得到以下错误: Msg: Illegal offset type in isset or empty and Msg: preg_match_all() expects parameter 2 to be string, array given
    • 您必须检查其中一项索引。
    【解决方案2】:

    您必须在表单标签中添加enctype="multipart/form-data"才能通过表单上传数据。

    【讨论】:

      【解决方案3】:

      所以我弄清楚了我的问题所在。或者更好的问题。

      我的第一个问题是我必须在表单标签中添加:enctype="multipart/form-data",正如其他人已经在 cmets 中指出的那样

      我的第二个问题是我必须在我的 autoload.php 文件中添加$autoload['libraries'] = array('database',.., 'upload');

      最后但同样重要的是,我将$this-&gt;load-&gt;library('upload', $config); 更改为$this-&gt;upload-&gt;initialize($config); 不知何故,配置无法正确初始化,所以这解决了我的最后一个问题。

      【讨论】:

        猜你喜欢
        • 2014-03-05
        • 2018-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-13
        • 2015-11-20
        • 1970-01-01
        相关资源
        最近更新 更多