Welcome Guest, Not a member yet? Register   Sign In
Delete checked files
#1

[eluser]rad11[/eluser]
Hi I have problem with deleting a checkedboxes files. My code dont delete them what I do wrong ?

Controller
Code:
public function remove_checked_images_files(){
    
$this->form_validation->set_rules('images[]', 'Private Message', 'required|xss_clean');

if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('error', '<p  red; font-weight:bold; font-size:20px;">Nie udalo sie usunac<p>');

redirect('media_ctrl/media_view');


}
else //success
{
$checked_messages = $this->input->post('images'); //selected messages

$this->model_media->delete_checked($checked_messages);
$this->model_media->delete_checked_files($checked_messages);

$this->session->set_flashdata('success', '<p  red; font-weight:bold; font-size:20px;">Udalo sie usunac<p>');
redirect('media_ctrl/media_view');


}
    
}

Model

Code:
public function delete_checked_files($message_ids){
        $this->db
                ->select('full_path')
                ->where_in('id_image', $message_ids);
              
        $count = count($message_ids);
        
        $query = $this->db->get('images');
        $deleted = FALSE;
        if ($query->num_rows() > 0)
        {
        
           $row = $query->row_array();
          
           for($i=0;$i<$count;$i++){
               unlink($row['full_path'][$i]);
              
           }
          
        }

      
    }
#2

[eluser]CroNiX[/eluser]
Try something like this:
Code:
public function delete_checked_files($message_ids){
  $files = $this->db
    ->select('full_path')
    ->where_in('id_image', $message_ids)
    ->get('images')
    ->result_array();
              
    if (count($files))
    {
        foreach($files as $file)
        {
           unlink($file['full_path']);
        }
    }
}
if that doesn't work, try echoing $file['full_path'] instead of unlink() so you can see if the path is actually correct.
#3

[eluser]rad11[/eluser]
Its not working . And when I echo
Code:
$file['full_path'];
show nothing. But when I var_dump a
Code:
$files
show
Code:
array(0)
.
#4

[eluser]CroNiX[/eluser]
do a var_dump of $message_ids as well as echo $this->db->last_query().
#5

[eluser]rad11[/eluser]
$message_ids are correctly.
and last query to are correctly.

have resolved the problem relied on the fact that in the controller had to change the order of removal to the

Code:
$ this-> model_media-> delete_checked_files ($ checked_messages); / / delete files
$ this-> model_media-> delete_checked ($ checked_messages) / / delete from db
#6

[eluser]CroNiX[/eluser]
If the $message_ids and the query is correct, then it doesn't make sense that $files would not contain any results. You said it showed array(0) when you dumped it.
#7

[eluser]CroNiX[/eluser]
Ah, glad you got it sorted.
#8

[eluser]rad11[/eluser]
I Thank you forced me to think. Smile




Theme © iAndrew 2016 - Forum software by © MyBB