如何在更新时从文件夹中删除旧文件?

时间:2017-04-28 06:39:20

标签: php codeigniter

我想从文件夹中删除旧文件,即,如果我更新图像,旧图像保留在文件夹中,并且上传新图像。

这里的更新很好,但经常更新后文件数量正在增加。

这是从数据库中选择内容以及更新

的代码
public function select_testimonials($id)
{
    $this->session->set_userdata('id',$id );
    $query = $this->cl_testimonials_model->select($id);
    $data1['selectdata'] = $query->result();
        // $this->index();
    $query = $this->db->get("cl_testimonials");
    $data1['records'] = $query->result();
    $this->load->view('admin/testimonials/testimonials_edit',$data1);
}



public function update_testimonials()   
{   
    $id = $this->input->post("id");
    if ($this->input->post('submit')=="update")
    {
        if(!empty($_FILES['picture']['name']))
        {
            $config['upload_path'] = 'uploads/images/testimonials';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['file_name'] = $_FILES['picture']['name'];

            //Load upload library and initialize configuration

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

            if($this->upload->do_upload('picture'))
            {
                $uploadData = $this->upload->data();
                $picture = $uploadData['file_name'];
            }

        }

        //Prepare array of user data
        $data = array('name' => $this->input->post('name'),'content' => $this->input->post('content'),'picture' => $picture);  

        //Pass user data to model
        $insertUserData = $this->cl_testimonials_model->update($id,$data);
        }
        //Storing insertion status message.
        if(isset($insertUserData))
             {
                $data['message']="* testimonials updated successfully.";
             }
             else
             {
                $data['message']="* Some problems occured, please try again.";
             }


    $query = $this->db->get("cl_testimonials");
    $data['records'] = $query->result();
    $this->load->view('admin/testimonials/testimonials_listing',$data); 
}

2 个答案:

答案 0 :(得分:0)

按ID获取现有的推荐图片路径,更新,然后使用unlink('old file path')

更多信息:Here

答案 1 :(得分:0)

使用unlink();在PHP中的功能。它也适用于Codeigniter。

unlink("target path/".$file name);