Boopathi N
Boopathi N

Reputation: 406

How to delete the specific image in folder using codeigniter?

I am using code-igniter. In my project i would like to delete the specific image in folder(artical_images). If i try to use delete_files() option it deletes all the images in the folder when use the code like,

public function delete_post($value='$id')
    {

        $query=$this->db->query("select artical_profile from articals where artical_id='$value' ");

        $img_name=$query->result()[0]->artical_profile;

        //echo $img_name;

        delete_files('./artical_images/',TRUE);

       // $this->db->where('artical_id',$value);
        //$this->db->delete('articals');

        exit();

    }

But when i include the specific image name taken from db and include with delete file path it doesn't work.The code is below,

public function delete_post($value='$id')
    {

        $query=$this->db->query("select artical_profile from articals where artical_id='$value' ");

        $img_name=$query->result()[0]->artical_profile;

        //echo $img_name;

        delete_files('./artical_images/$img_name',TRUE);

       // $this->db->where('artical_id',$value);
        //$this->db->delete('articals');

        exit();

    }

even i included the realpath option it also not works,

public function delete_post($value='$id')
    {

        $query=$this->db->query("select artical_profile from articals where artical_id='$value' ");

        $img_name=$query->result()[0]->artical_profile;

        $img_name=realpath(APPPATH . '/artical_images/$img_name');

        delete_files('$img_name',TRUE);

        echo $img_name;

       // $this->db->where('artical_id',$value);
        //$this->db->delete('articals');

        exit();

    }

please give some idea to delete the files,

Upvotes: 0

Views: 994

Answers (2)

Rajen Antarkar
Rajen Antarkar

Reputation: 162

$this->load->helper("file");
delete_files($path);

Upvotes: 1

Ali
Ali

Reputation: 1438

delete_files uses a directory path to delete all the files in the directory. It applies the recursive unlink command to delete files.

To delete particular file, You can use unlink with the proper file path.

e.g. unlink(FILE_PATH);

Upvotes: 1

Related Questions