“活动图像”字段在CodeIgniter中是必需的

时间:2018-12-17 14:34:34

标签: codeigniter-3

我正在尝试在代码项目中上传图片。在图片字段中,我得到

  

“活动图像字段为必填。”

下面的代码是我的Activity_controller

public function create_activity(){
//...Upload the image
//... path to upload image
  $config['upload_path'] = './assets/images/uploads'; // The path to upload the image
  $config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|doc';
  $config['max_size'] = '2048';
  $config['max_width'] = '500';
  $config['max_height'] = '500';

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

  if(!$this->upload->do_upload()) {
    $error = array('error' => $this->upload->display_errors());
    $activity_image = 'noimage.jpg';
  } else {
    $data = array('upload_data' => $this->upload->data());
    $activity_image = $_FILES['userfile']['name'];
  }

  $this->activity_model->save_activity($activity_image);
  redirect('activity_controller');
}       

活动模型,在数据库中插入活动这只是在数据库中插入数据的功能。

public function save_activity($activity_image){
//... code to insert others data in the database

  'activity_image' => $activity_image
                            );
                            return $this->db->insert('activity', $data);
                        }

下面的代码是视图文件夹中的create_activities.php文件:

                    <?php echo validation_errors(); ?>
                    <?php echo form_open_multipart('activity_controller/create_activity');  ?>
                    <div class="form-group">
                        <label>Upload image</label>
                        <input name="userfile" type="file">
                        </div>  

我已将模型加载到autoload.php文件中。 我还使用了<input name="activity_image type="file">

“ image_name”是数据库表中字段的名称

请有人可以帮助我解决此问题?感谢您的帮助

提前谢谢

1 个答案:

答案 0 :(得分:0)

我认为您忘记了在表单文件的视图文件中添加(enctype =“ multipart / form-data”)

在此使用此代码。希望这对您有帮助。

<form action="/action_page_binary.asp" method="post" enctype="multipart/form-data">
  First name: <input type="file" name="name"><br>
  <input type="submit" value="Submit">
</form>