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

时间:2017-11-26 06:47:54

标签: php image codeigniter upload

我尝试将图片上传到文件路径并将该路径插入数据库。但是,上传始终会显示错误您没有选择要上传的文件 ..尽管我这样做了。

这是我的视图

setup(name='dnd31',
  version='0.1',
  packages=find_packages(),
  description='example to run keras on gcloud ml-engine',
  author='me',
  author_email='author@example.com',
  license='MIT',
  install_requires=[
      'keras',
      'h5py',
       'pandas',
       'ta-lib',
       'TA-Lib'],
  zip_safe=False)

CONTROLLER

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

MODEL

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

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分: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";
        }
    }
}

答案 3 :(得分:0)

这应该怎么做:

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

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