如何在Codeigniter中上传图像文件

时间:2018-02-21 07:36:27

标签: php codeigniter image-uploading

我无法上传图片文件,我不知道输入文件但无法检测到。这就是我无法上传文件的原因。 这是控制器,我把它命名为desain.php。

defined('BASEPATH') OR exit('No direct script access allowed');

class Desain extends CI_Controller {

public function index()
{
    $query = "select * from result,kategori where result.id_cat=kategori.id_cat";
    $tampil = $this->db->query($query)->result();
    $this->load->view('Admin/data.php', ['tampil' => $tampil]);
}

public function do_upload()
{
        $config['upload_path']      = './asset/upload/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']         = '2048000';
        $config['max_width']        = '1024';
        $config['max_height']       = '1024';
        $config['file_name']        = $this->input->post('photo');
        $this->load->library('upload',$config);

        if(!$this->upload->do_upload('photo'))
        {
            $error = array ('error' =>$this->upload->display_errors());

            redirect('admin/desain');
        }
        else { $data = $this->upload->data($config);
            $data = array('photo'       =>$this->upload->data($config),
                          'nama_desain' =>$this->input->post('nama'),
                          'id_cat'      =>$this->input->post('categori'),
                          'ket'         =>$this->input->post('ket'),
            );
            $this->db->insert('result',$data);
            redirect('admin/desain');
        }
}

}

这是观点:

 <form role="form" action="<?php echo base_url();?>admin/desain/do_upload" method="post" enctype="multipart/form-data">
  <div class="box-body">
    <div class="form-group">
      <label for="exampleInputEmail1">Nama Desain</label>
      <input type="text" class="form-control" name ="nama" id="nama" placeholder="Nama Desain">
    </div>
    <div class="form-group">
      <label for="exampleInputPassword1">Kategori</label>
      <select class="form-control" id="categori" name="categori">
            <option value="">Pilih Kategori</option>
            <option value="1">Vektor</option>
            <option value="2">Editing</option>
            <option value="3">Desain</option>
      </select>
    </div>
    <div class="form-group">
      <label for="exampleInputPassword1">Keterangan Gambar</label>
       <textarea class="form-control" rows="3" name="ket" id="ket" placeholder="Enter ..."></textarea>
    </div>
    <div class="form-group">
      <label for="exampleInputFile">File input</label>
      <input type="file" required="required" id="photo" name="photo" class="form-control">
    </div>

  </div>
  <!-- /.box-body -->

  <div class="box-footer">
    <button type="submit" class="btn btn-primary">Submit</button>
  </div>
</form>

我试图发布此表单,但此文件输入为NULL。

4 个答案:

答案 0 :(得分:1)

试试这个希望它会帮助你

$config['upload_path'] = './asset/upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']     = 2048000;
$config['max_width'] = 1024;
$config['max_height'] = 1024;
$config['file_name']        = $this->input->post('photo');
$this->load->library('upload', $config);
if (!$this->upload->do_upload($config['file_name'])) 
{
    $error = $this->upload->display_errors();
    //coding...

}
else 
{
    $filename = $this->upload->data();
    $data[$config['file_name']] = $filename['file_name'];
    //coding...
}

答案 1 :(得分:1)

您应该通过

捕获文件
$config['file_name'] = $_FILES['name']['photo'];

您将能够获取要处理的图像文件。

之前请检查该字段是否能够通过

获取
if(!empty($_FILES['name']['photo']))

这完全适用于我的电脑。希望它对你有所帮助。

答案 2 :(得分:1)

您无需执行此操作:$config['file_name'] = $this->input->post('photo');

默认情况下,上传课程会使用图片名称作为最终图片的名称。如果你想这样做,那么$_FILES['photo']['name'];就可以了(但同样,没有必要)。

此外,您无法正确获取上传照片的价值。我不确定你为什么要把配置数组放在这个函数中:$this->upload->data($config);但是这不应该如何使用。

如果您想获取文件名,您可以这样做:

$filename = $this->upload->data('file_name');

OR

$up_data = $this->upload->data();
$filename = $up_data['file_name']

请参阅此参考:https://www.codeigniter.com/userguide3/libraries/file_uploading.html#CI_Upload::data

此外,您没有对此行做任何事情:$error = array ('error' =>$this->upload->display_errors());。我建议在重定向之前将错误设置为flash消息。

答案 3 :(得分:0)

if(!$this->upload->do_upload('photo'))
{
    $error = array ('error' =>$this->upload->display_errors());
    redirect('admin/desain');
} else { 
    $uploadData = $this->upload->data();
    $picture = $uploadData['file_name'];
    $data = array(
        'photo' => $uploadData['file_path'].'upload_images/'. $picture,
        'nama_desain' =>$this->input->post('nama'),
        'id_cat' =>$this->input->post('categori'),
        'ket' =>$this->input->post('ket'),
    );
    $this->db->insert('result',$data);
    redirect('admin/desain');
}