Codeigniter图片未上传

时间:2018-11-02 09:08:27

标签: php codeigniter

我正在尝试编辑将图像上传到我的数据库。但是,图像字段未获取该值,因此字段为空。

视图:

            <?php foreach($blog as $b){?>
                <form method="post" action="<?php echo site_url('adminUpdatePost/'.$b->id); ?>" class="col s12">     
                    <div class="row">
                        <div class="input-field col s12">
                            <textarea id="textarea1" name="textarea1" class="materialize-textarea"><?=$b->post?></textarea>
                            <label for="textarea1">Post</label>
                        </div>
                    </div>

                    <div class="file-field input-field">
                       <div class="btn">
                            <span>Post Image</span>
                            <input type="file" name="file">
                        </div>
                        <div class="file-path-wrapper">
                            <input id="image" name="image" class="file-path validate" placeholder="<?=$b->image?>" type="text">
                      </div>
                    </div>

                    <input type="submit" value="Submit">                    
                </form>
                <?php } ?>

控制器:

public function updatePost($id)
{
    $config = array(
        'upload_path' => 'uploads/',
        'allowed_types' => 'gif|jpg|jpeg|png',
        'max_size' => 0,
        'filename' => url_title($this->input->post('file')),
        'encrypt_name' => false,
    );

    $this->load->library('upload', $config);
    $this->upload->do_upload('file');

    $post=array(         
        'post'=>$this->input->post('textarea1'), 
        'image'=>$this->upload->file_name,           //image 
    ); 

    ChromePhp::log("edit data : " . json_encode($post));

    $result = $this->AdminModel->updatePost($post, $id);    
}

使用ChromePhp检查$ post数组的内容时,“图像”显示为空值。但是“职位”具有价值。

我找不到哪里出了问题。我在哪里弄错了?

3 个答案:

答案 0 :(得分:1)

请使用

 if ( ! $this->upload->do_upload('file'))
                {
                      echo "<pre>";print_r($this->upload->display_errors());die();

                }
                else
                {
                     echo "nothing wrong";
                }

并告诉我结果。如果这样没有帮助,请尝试更改

 <input type="file" name="file">

到  

并将$this->upload->do_upload('file')更改为$this->upload->do_upload('userfile')

答案 1 :(得分:1)

要上传图像/文件,您需要在表单标签中添加enctype属性

应该是这样

<form method="post" action="<?php echo site_url('adminUpdatePost/'.$b->id); ?>" class="col s12" enctype="multipart/form-data>
  <div class="row">
      <div class="input-field col s12">
          <textarea id="textarea1" name="textarea1" class="materialize-textarea"><?=$b->post?></textarea>
          <label for="textarea1">Post</label>
      </div>
  </div>

  <div class="file-field input-field">
     <div class="btn">
          <span>Post Image</span>
          <input type="file" name="file">
      </div>
      <div class="file-path-wrapper">
          <input id="image" name="image" class="file-path validate" placeholder="<?=$b->image?>" type="text">
    </div>
  </div>

  <input type="submit" value="Submit">  
</form>

答案 2 :(得分:0)

您必须在表单年龄中指定“ enctype =” multipart / form-data“” 类型

<form method="post" action="<?php echo site_url('adminUpdatePost/'.$b->id); ?>" class="col s12" enctype="multipart/form-data">