Codeigniter 3更新后期图像功能失败

时间:2018-04-28 12:04:05

标签: php codeigniter-3

我正在使用Codeigniter 3.1.8中的基本博客应用程序

创建帖子更新帖子功能。

正如人们所料, update 发布功能的许多代码都来自创建功能的代码。

create.php视图:

<?php echo form_open_multipart ("posts/create"); ?>
 <div class="form-group <?php if(form_error('title')) echo 'has-error';?>">
  <input type="text" name="title" id="title" class="form-control" placeholder="Title">
  <?php if(form_error('title')) echo form_error('title'); ?> 
 </div>
 <div class="form-group <?php if(form_error('desc')) echo 'has-error';?>">
  <input type="text" name="desc" id="desc" class="form-control" placeholder="Short decription">
  <?php if(form_error('desc')) echo form_error('desc'); ?> 
 </div>
 <div class="form-group <?php if(form_error('body')) echo 'has-error';?>">
  <textarea name="body" id="body" cols="30" rows="5" class="form-control" placeholder="Add post body"></textarea>
  <?php if(form_error('body')) echo form_error('body'); ?> 
 </div>
 <div class="form-group">
  <select name="category" id="category" class="form-control">
    <?php foreach ($categories as $category): ?>
    <option value="<?php echo $category->id; ?>"><?php echo $category->name; ?></option>
    <?php endforeach; ?>
  </select>
 </div>
 <label for="postimage">Upload an image</label>
 <div class="form-group">
  <input type="file" name="userfile" id="postimage" size="20">
 </div>
 <div class="form-group">
  <input type="submit" value="Save" class="btn btn-block btn-md btn-success">
 </div>
 <?php echo form_close(); ?>

update.php视图:

<?php echo form_open_multipart("posts/update"); ?>
 <input type="hidden" name="id" id="pid" value="<?php echo $post->id; ?>">
 <div class="form-group <?php if(form_error('title')) echo 'has-error';?>">
  <input type="text" name="title" id="title" class="form-control" placeholder="Title" value="<?php echo $post->title; ?>">
  <?php if(form_error('title')) echo form_error('title'); ?> 
 </div>
 <div class="form-group <?php if(form_error('desc')) echo 'has-error';?>">
  <input type="text" name="desc" id="desc" class="form-control" placeholder="Short decription" value="<?php echo $post->description; ?>">
  <?php if(form_error('desc')) echo form_error('desc'); ?> 
 </div>
 <div class="form-group <?php if(form_error('body')) echo 'has-error';?>">
  <textarea name="body" id="body" cols="30" rows="5" class="form-control" placeholder="Add post body"><?php echo $post->content; ?></textarea>
  <?php if(form_error('body')) echo form_error('body'); ?> 
 </div>
 <div class="form-group">
  <select name="category" id="category" class="form-control">
    <?php foreach ($categories as $category): ?>
    <?php if ($category->id == $post->cat_id): ?>
    <option value="<?php echo $category->id; ?>" selected><?php echo $category->name; ?></option>
    <?php else: ?>
    <option value="<?php echo $category->id; ?>"><?php echo $category->name; ?></option>
    <?php endif; ?>
    <?php endforeach; ?>
  </select>
 </div>
 <label for="postimage">Upload an image</label>
 <div class="form-group">
  <input type="file" name="userfile" id="postimage" size="20">
 </div>
 <div class="form-group">
  <input type="submit" value="Save" class="btn btn-block btn-md btn-success">
 </div>
 <?php echo form_close(); ?>

创建帖子的方法有一个非常有效的图片上传功能:

控制器:

public function create() {
    $data = $this->Static_model->get_static_data();
    $data['tagline'] = "Add New Post";
    $data['categories'] = $this->Categories_model->get_categories();

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('desc', 'Short description', 'required');
    $this->form_validation->set_rules('body', 'Body', 'required');
    $this->form_validation->set_error_delimiters('<p class="error">', '</p>');

    if($this->form_validation->run() === FALSE){
        $this->load->view('partials/header', $data);
        $this->load->view('create');
        $this->load->view('partials/footer');
    } else {
        // Upload image
        $config['upload_path'] = './assets/img/posts';
        $config['allowed_types'] = 'jpg|png';
        $config['max_size'] = '2048';

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

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

        $this->Posts_model->create_post($post_image);
        redirect('posts');
    }
}

型号:

public function create_post($post_image) {
    $data = [
        'title' => $this->input->post('title'),
        'description' => $this->input->post('desc'),
        'content' => $this->input->post('body'),
        'post_image' => $post_image,
        'author_id' => 3,
        'cat_id' => $this->input->post('category'),
        'created_at' => date('Y-m-d H:i:s')
    ];
    return $this->db->insert('posts', $data);
}

我试图在帖子更新中“复制”图片上传功能:

在帖子控制器中:

public function update() {
    // Form data validation rules
    $this->form_validation->set_rules('title', 'Title', 'required',  array('required' => 'The %s field can not be empty'));
    $this->form_validation->set_rules('desc', 'Short description', 'required',  array('required' => 'The %s field can not be empty'));
    $this->form_validation->set_rules('body', 'Body', 'required',  array('required' => 'The %s field can not be empty'));
    $this->form_validation->set_error_delimiters('<p class="error">', '</p>');

    $id = $this->input->post('id');
    // Upload image
    $config['upload_path'] = './assets/img/posts';
    $config['allowed_types'] = 'jpg|png';
    $config['max_size'] = '2048';

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

    $data = array('upload_data' => $this->upload->data());
    $post_image = $_FILES['userfile']['name'];

    if ($this->form_validation->run()) {
        $this->Posts_model->update_post($id, $post_image);
        redirect('posts/post/' . $id);
    } else {
        $this->edit($id);
    }
}

在Posts_model模型中:

public function update_post($id, $post_image) {
    $data = [
        'title' => $this->input->post('title'),
        'description' => $this->input->post('desc'),
        'content' => $this->input->post('body'),
        'post_image' => $post_image,
        'cat_id' => $this->input->post('category')
    ];

    $this->db->where('id', $id);
    return $this->db->update('posts', $data);
}

上面的代码正确地将文件名插入到posts表中,但不会在./assets/img/posts上传图片。

我哪里错了?

1 个答案:

答案 0 :(得分:0)

globalcreate_post之间的区别在于您在update_post而不是$id中将$dataupdate_post作为参数传递,就像你在$post_image中所做的那样。

create_post

所以我想它会把ID放在名字中。 试试:

$this->Posts_model->update_post($id, $data);