【问题标题】:How to upload image file in Codeigniter如何在 Codeigniter 中上传图像文件
【发布时间】:2018-02-21 07:36:27
【问题描述】:

我无法上传图像文件,我不知道输入文件也无法检测到它。这就是为什么我无法上传文件的原因。 这是控制器,我将其命名为 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。

【问题讨论】:

  • 嗨,我已经尝试了我的代码的所有答案,但仍然出错。错误是“上传路径似乎无效”。那么,我该怎么办?

标签: php codeigniter image-uploading


【解决方案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...
}

【讨论】:

  • post('photo') 到底在哪里填充? ;)
【解决方案2】:

您应该通过

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

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

在此之前请检查该字段是否可以通过

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

这在我的电脑上完美运行。希望对您有所帮助。

【讨论】:

  • 默认上传类会使用图片的名字作为最终图片的名字。我相信语法也是$_FILES['photo']['name']
【解决方案3】:

你不需要这样做:$config['file_name'] = $this-&gt;input-&gt;post('photo');

默认情况下,上传类将使用图像的名称作为最终图像的名称。如果您想这样做,那么$_FILES['photo']['name']; 将起作用(但同样,有不需要)。

此外,您没有正确获得上传照片的价值。我不确定你为什么将配置数组放在这个函数中:$this-&gt;upload-&gt;data($config);,但这不是它应该被使用的方式。

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

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

$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' =&gt;$this-&gt;upload-&gt;display_errors());。我建议在重定向之前将错误设置为 Flash 消息。

【讨论】:

    【解决方案4】:
    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');
    }
    

    【讨论】:

    • 请在您的回答中添加一些解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2014-01-04
    • 2016-11-30
    相关资源
    最近更新 更多