繁体   English   中英

CodeIgniter:将图像上传到路径和数据库

[英]CodeIgniter: Upload Image to path and database

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

这是我的观点

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

CONTROLLER

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

如下更改您的上传代码。 不要使用$this->input->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();
    } 

如果您愿意,可以尝试一下。

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

这应该如何做:

$this->upload->do_upload('profilePic')

do_upload函数为您执行必要的发布。

暂无
暂无

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

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