【问题标题】:CodeIgniter: Upload Image to path and databaseCodeIgniter:将图像上传到路径和数据库
【发布时间】:2018-05-09 16:12:14
【问题描述】:

我正在尝试将图像上传到文件路径并将该路径插入数据库。但是,上传总是显示错误您没有选择要上传的文件 ..即使我选择了。

这是我的观点

<form action="<?= site_url('profile/profile_submit')?>" method="post" enctype="multipart/form-data">
       <input type="file" class="image-upload" accept="image/*" name="profilePic" id="profilePic"/>
</form>

控制器

$config['upload_path'] = 'assets/img/profile_img/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['overwrite'] = TRUE;
$config['max_size'] = "2048000"; 
$config['max_height'] = "768";
$config['max_width'] = "1024";
$this->load->library('upload', $config);
    if ($this->upload->do_upload('profilePic')){
        $data = $this->upload->data();
            $picture = array(
                'photoPath' => $this->upload->data('full_path').$data['file_name']
            );
    }
    else{
            echo $this->upload->display_errors();
    } 

$this->profile_model->submit_profile($picture);

型号

function submit_profile($picture){
        $this->db->insert('tbl_st_picture', $picture);
        }

【问题讨论】:

    标签: php image codeigniter upload


    【解决方案1】:
    public function add() { 
        $this->load->helper('url');
        $config['upload_path'] = "./assets/uploads/";
        $config['allowed_types'] = 'gif|jpeg|png|jpg';
        $config['max_height'] = '1000';
        $config['max_width'] = '2048';
        $config['max_size'] = '2048';
        $this->load->library('upload',$config);
        $this->path = './assets/uploads/';
    
       $this->upload->initialize($config);
    
        if (! $this->upload->do_upload('berkas'))
        {
            $error = array('error' => $this->upload->display_errors());
            redirect(base_url().'berita/tambah');
        }else{
            $dataUpload = $this->upload->data();
    
            $data = array(
                'Judul' => $this->input->post('Judul'),
                'Foto' => $dataUpload['file_name'],
                'Isi' => $this->input->post('Isi')
                );
            $this->load->model('berita_model');
            $result = $this->berita_model->insert('berita',$data);
    
            if ($result>0) {
                redirect(base_url() .'news');
            } else {
                echo "Gagal";
            }
        }
    }
    

    【讨论】:

    • 函数插入($db,$data){ $result = $this->db->insert($db,$data);返回$结果; }
    • 这是我的模特
    【解决方案2】:

    如下更改您的上传代码。不要使用$this-&gt;input-&gt;post() 来获取文件数据。还要确保您在表单中添加了enctype="multipart/form-data"

    if ($this->upload->do_upload('profilePic')){
           $data = $this->upload->data();
            $picture = array(
                    'photoPath' => $this->upload->data('full_path').$data['file_name'],
            );
        }
        else{
                echo $this->upload->display_errors();
        } 
    

    【讨论】:

    • 我正在使用助手,我正在使用这个 form_open_multipart('profile/profile_submit')
    • 您会在视图中显示更新后的代码和表单标签吗?
    【解决方案3】:

    如果你愿意,你可以试试这个。

    function file_upload()
    {
        $config['upload_path'] = './uploads/';
    
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
    
        $this->load->library('Upload', $config);
    
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
    
            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
    
            $this->load->view('upload_success', $data);
        }
    }
    

    【讨论】:

    • 创建文件夹名上传到你项目的根路径中。
    【解决方案4】:

    这应该怎么做:

    $this-&gt;upload-&gt;do_upload('profilePic')

    do_upload 函数会为您完成必要的发布。

    【讨论】:

    • 试过 $this->upload->do_upload('profilePic') ,还是一样的错误。我更新了上面的代码
    • 好吧,我将您的 exact 代码复制并粘贴到我的本地主机 CI 中,它工作正常并且图像上传(您是否验证图像是否在文件夹中?)。请注意,如果您使用上传代码刷新控制器页面,您当然会收到错误,因为那时没有发布数据。对于photoPath,您只需执行$data['full_path'],它已经包含文件名。
    • 另请注意,如果assets/img/profile_img/ 不存在,它也会失败(尽管我不确定错误消息是什么)。 CI 不会创建上传文件夹,您必须自己创建。
    • assets/img/profile_img/ 存在。我已经改变了 $data['full_path'].仍然没有上传;
    猜你喜欢
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 2016-09-05
    相关资源
    最近更新 更多