Codeigniter 3博客应用程序:update()方法删除缩略图

时间:2018-04-29 12:00:45

标签: codeigniter file-upload

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

创建帖子更新帖子功能。

创建帖子后,可以选择上传帖子缩略图,否则会显示默认图片。在帖子控制器中,有创建帖子的方法

public function create() {

    // More code here

    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 update() {
    // Form data validation rules
    // irrelevant for the question suppressed 

    $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());
    $this->upload->do_upload();
    $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);
}

编辑帖子查看

 <?php echo form_open_multipart(base_url('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(); ?>

update()方法的问题在于,在编辑帖子时,除非我用新的缩略图替换现有的缩略图(换句话说,如果我让文件上传字段为空),导致离开帖子没有缩略图

<img src="http://localhost/ciblog/assets/img/posts/">

我尝试过的(不是最好的主意,我承认)是以这种方式获取 posts 表中已存在的文件的名称:

$data['post'] = $this->Posts_model->get_post($id);
$post_image = $data['post']->post_image;

但是它会给出错误Trying to get property of non-object

我做错了什么?

1 个答案:

答案 0 :(得分:1)

解决了它:

在编辑帖子表单中,我在文件输入上方添加了一个隐藏类型的输入,以便&#34;捕获&#34;帖子图像来自数据库

<input type="hidden" name="postimage" id="postimage" value="<?php echo $post->post_image; ?>">
<label for="postimage">Upload an image</label><div class="form-group">
   <input type="file" name="userfile" id="postimage" size="20">
</div>

在Posts控制器中,我已经替换了

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

with:

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

上面的代码表示:如果没有上传新图像,请再次在posts表中写入现有的名称,而不是写 nothing 。如果上传了新照片,请写下照片的名称。