Search code examples
mysqlcodeignitercodeigniter-3

how to delete multiple files (codeigniter), according to a specific id?


Hello there i am currently trying to unlink() multiple files from folder with specific category id, getting the right files (names) is no problem und how to unlink a file i know too, but i have a hard time to loop over the result array from the query and then delete not all files but those files out of the query, here is my code:

/**
 * delete category by id
 * @param $id category_id
 * @return boolean 
 */
public function delete_images($id){

    $this->db->select('post_image');
    $query = $this->db->get_where('posts', array('category_id' => $id));
    $images = $query->result_array();

    if (!empty($images)){
      if(!in_array('default_image', $images)){
        foreach ($images as $image) { 
            unlink(FCPATH . 'assets/images/posts/' . $image);
        }
      }
    } else {
      $this->db->query("DELETE FROM categories WHERE categories.id = $id");
    }
}

Help is appreaciated, thanks.

The actual result is:

Message: Array to string conversion
Filename: models/Category_model.php
Line Number: 71





Solution

  • I think your code should like

    unlink('assets/images/posts/' . $image['post_image']);
    

    You missed the index & its become an array but string was expected there