如何使用 Codeigniter 框架上传图片?

里亚兹

我需要有关使用 CodeIgniter 上传图片代码的帮助。请参考我下面的代码

我确实尝试过 File Upload 类,但我无法在已创建的代码上使用它来添加新产品。

这是我的表格

<form action="<?php echo base_url().'index.php/admin/Welcome/add_new_product'; ?>" method="POST">
  <section class="content">
    <div class="row">
      <div class="col-md-12">
        <div class="box box-info">
          <div class="box-header">
            <h3 class="box-title">Product description
              <small></small>
            </h3>


            <h1><input type="textarea" name="pd_title" placeholder="Enter Title Here" style="width:100%;"></h1>
          </div>
          <!-- /.box-header -->
          <div class="box-body pad">

                  <textarea id="editor1" name="pd_short_desc" rows="10" cols="80">
                                          This is my textarea to be replaced with CKEditor.
                  </textarea>
                </div>
                  <div class="box-body pad">
                  <label for="Image">Upload Image</label>
                  <input type="file" name="pd_img" value="" style="padding:10px;">
                </div>

                  <div class="box-body pad">
                  <h3 class="box-title">Description to right of Image</h3>
                  <textarea id="editor2" name="pd_info_right" rows="10" cols="80">
                                          This is my textarea to be replaced with CKEditor.
                  </textarea>
                </div>
                  <div class="box-body pad">
                  <h3>Product Complete Description</h3>
                  <textarea id="editor3" name="pd_desc" rows="10" cols="80">
                                          This is my textarea to be replaced with CKEditor.
                  </textarea>
                </div>

                <input type="submit" name="submit" class="btn  btn-info btn-raised ink-reaction">
            </form>

这是我的控制器

public function add_new_product()
    {
        $p = $this->model_one->add_new_product();
        redirect('admin/Welcome/view_products');
    }

此模型应在 mysqli db 表中插入数据

public function add_new_product()
{
  $pd_title = $_POST['pd_title'];
  $pd_short_desc = $_POST['pd_short_desc'];
  $pd_image = $_POST['pd_image'];
  $pd_info_right = $_POST['pd_info_right'];
  $pd_desc = $_POST['pd_desc'];

  $sql = $this->db->query("INSERT INTO products (pd_title, pd_short_desc, pd_image, pd_info_right, pd_desc)
  VALUES('$pd_title','$pd_short_desc', '$pd_image','$pd_info_right','$pd_desc')");

  return "yes";
}

提交此表单时,Image 需要上传到uploads 文件夹中,并插入到mysqli db 中的products 表中。

丹麦阿里

做一些改变

看法

enctype="multipart/form-data"<form>标签中添加

<form action="<?php echo base_url().'index.php/admin/Welcome/add_new_product'; ?>" method="POST" enctype="multipart/form-data">

控制器

获取POST控制器中的值

public function add_new_product()
{
    $data = array('pd_title' => $_POST['pd_title'], 'pd_short_desc' => $_POST['pd_short_desc'], 'pd_info_right' => $_POST['pd_info_right'], 'pd_desc' => $_POST['pd_desc']);

    $config['upload_path'] = FCPATH.'uploads/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = 100; 
    $config['max_width'] = 1024; 
    $config['max_height'] = 768;

    if(isset($_FILES['pd_img']['name'])){
       $this->load->library('upload', $config);
       if(!$this->upload->do_upload('pd_img')){ 
           print_r($this->upload->display_errors());
       }else{
           $data['pd_image'] = $this->upload->data('file_name');
       }
    }

    $this->model_one->add_new_product($data);
    redirect('admin/Welcome/view_products');
}

模型

public function add_new_product($data)
{
    return $this->db->insert('products', $data);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章