我如何继续更新图像或文件,但仍然不删除或丢失另一个文件? PHP Codeigniter

问题描述 投票:0回答:1

我正在更新我的图像或文件,但是其他图像(如果提交)也将被删除。即使更新单个文件,如何保存这些图像?

[我的控制器-因此,在这里我将文件或图像置于内部,以便可以将其插入数据库中]

public function edit_images($id){
        // Upload Multiple Images

        $number_of_file = sizeof($_FILES['userfile']['tmp_name']);
        $file = $_FILES['userfile'];

        $files = array();

        // Faking upload calls to $_FILE
        for ($i = 0; $i < $number_of_file; $i++) :

            $_FILES['userfile']['name']     = $file ['name'][$i];
            $_FILES['userfile']['type']     = $file ['type'][$i];
            $_FILES['userfile']['tmp_name'] = $file ['tmp_name'][$i];
            $_FILES['userfile']['error']    = $file ['error'][$i];
            $_FILES['userfile']['size']     = $file ['size'][$i];

            $config['upload_path'] = './assets/images/activities';
            $config['allowed_types'] = 'gif|jpg|png';
            $this->load->library('upload', $config);

            $this->upload->initialize($config);
            $this->upload->do_upload('userfile');

            //$data = $this->upload->data();

            $files[] = $this->upload->data('file_name');

            //$data= implode(",",$userfile);
            //$this->blog->blog_img($data);

                //redirect('/admin/blog/img/insert');
        endfor;

        $data= implode(",",$files);

        $this->post_model->edit_images($id, $data);
        redirect('posts/edit/'.$id);
    }

我的模特

public function edit_images($id, $files){
    $field_question = array(
        'act_photo' => $files,
    );

    $this->db->where('id', $id);
    return $this->db->update('questions', $field_question);
}

和我的视图-我分解了文件,以便可以唯一地获取它们

<?php echo form_open_multipart('posts/edit_images/'.$questions['id']); ?>

        <?php foreach($activities as $activity): ?>

            <?php   $images=explode(',', $activity['act_photo']);
            foreach($images as $key => $image): ?>
                  <table class="table">
                  <tr>
                       <td><img style="border: 2px solid black;" class="img-responsive" src="<?php echo base_url('assets/images/activities/').$image;?>" /></td>
                       <td><input type="file" name="userfile[]" size="20" /></td>
                      <input type="text" name="imgFile" value="<?php echo $image ?>" />
                   </tr>
                </table>
            <?php endforeach; ?> 
    <?php endforeach; ?>

<button type="submit" class="btn btn-block btn-dark btn-md"><h6>Change Images</h6></button>

php codeigniter codeigniter-3
1个回答
0
投票

您可以用act_photo字符串中的新图像路径替换以前的图像路径。

HTML

<tr>
   <td>
        <img style="border: 2px solid black;" class="img-responsive" src="<?php echo base_url('assets/images/activities/').$image;?>" />
    </td>
   <td>
          <input type="file" name="userfile[]" size="20" />
    </td>
    <!-- passing previous images string -->
       <input type="hidden" name="images" value="<?php echo $activity['act_photo'] ?>" />
    <!-- passing this single image path -->
        <input type="hidden" name="imgFile" value="<?php echo $image ?>" />
</tr>

控制器

public function edit_images($id){
    //add following lines to the controller
    $prev_image = $this->input->post("imgFile"); //you can unlink images besed on this string
    $prev_images = $this->input->post("images");
    $cur_images = implode(",",$files);

    $images = str_replace(",".$prev_image.",",$cur_images,prev_images); //search for ,image, format
    $images = str_replace($prev_image.",",$cur_images,images); //search for image, format
    $images = str_replace(",".$prev_image,$cur_images,images); //search for ,image format
//you can preform exact above with regex with less code.

    $this->post_model->edit_images($id,$images);
}
© www.soinside.com 2019 - 2024. All rights reserved.