Welcome Guest, Not a member yet? Register   Sign In
Cant delete, a single file
#1

[eluser]rad11[/eluser]
I cant delete a file when using unlink

view
Code:
<?php
    $query = $this->model_media->gallery();
    foreach($query->result() as $row){
        echo anchor($row->image_url, "<img  height: 100px;'>image_url' />");
        echo anchor('media_ctrl/remove_image/' . $row->id_image, 'X');
        echo form_checkbox('images[]', $row->id_image);
    }
    ?&gt;

model

Code:
public function get_image_name_by_id($id_image){
        
        $this->db->select('full_path');
        $this->db->where('id_image', $id_image);
        $query = $this->db->get('images');
        
        if ($query->num_rows() > 0)
        {
           $row = $query->row();
           echo $row->full_path;
        }
        
        
    }
    public function remove_file($id_image){
        
        $this->model_media->get_image_name_by_id($id_image);
      
        unlink($row->full_path);
        
        echo '<pre>';
        var_dump($this->model_media->get_image_name_by_id($id_image));
        echo '<br />';
        echo '</pre>';
        die();
    }

Controller

Code:
public function remove_image($id_image){
    if($this->session->userdata('username') == 'admin'){
        
        $this->model_media->remove_file($id_image);
        $this->model_media->remove_image($id_image);
        
        $this->session->set_flashdata('success', '<p  red; font-weight:bold; font-size:20px;">Udalo sie usunac<p>');
        
        redirect('media_ctrl/media_view');
        }else{
        redirect('home/restricted');
        }
}

script delete only in database but not in a folder Sad
#2

[eluser]rad11[/eluser]
Its work but can stay like this?

Model
Code:
public function remove_file($id_image){
        
        $this->db->select('full_path');
        $this->db->where('id_image', $id_image);
        $query = $this->db->get('images');
        
        if ($query->num_rows() > 0)
        {
           $row = $query->row();
           echo $row->full_path;
        }
        unlink($row->full_path);

        
    }

Controller

Code:
public function remove_image($id_image){
    if($this->session->userdata('username') == 'admin'){
        
        $this->model_media->remove_file($id_image);
        $this->model_media->remove_image($id_image);
        
        $this->session->set_flashdata('success', '<p  red; font-weight:bold; font-size:20px;">Udalo sie usunac<p>');
        
        redirect('media_ctrl/media_view');
        }else{
        redirect('home/restricted');
        }
}
#3

[eluser]rad11[/eluser]
Next question is how i can delete checked files from folder? Any idea?
#4

[eluser]CroNiX[/eluser]
You're echoing here instead of returning the value:
Code:
if ($query->num_rows() > 0)
{
    $row = $query->row();
    echo $row->full_path;
}

unlink() returns a boolean TRUE upon success, and FALSE upon failure. Maybe you should test that before deleting from your db? Return the status of unlink() from that function, so that you can test it before deleting from the db.

Code:
public function remove_file($id_image)
{        
        $this->db->select('full_path');
        $this->db->where('id_image', $id_image);
        $query = $this->db->get('images');
        $deleted = FALSE;
        if ($query->num_rows() > 0)
        {
           $row = $query->row();
           $deleted = unlink($row->full_path);
        }
        return $deleted;  //return unlink() status
    }

Then in your controller
Code:
public function remove_image($id_image){
    if($this->session->userdata('username') == 'admin'){
        
        $deleted = $this->model_media->remove_file($id_image);
        if ( ! $deleted)
        {
          //error deleting file, do something about it
        }
        else
        {
          //file was deleted, now attempt delete from db
          $deleted_from_db = $this->model_media->remove_image($id_image);  //this model should test whether it was actually deleted, and return TRUE if it did or FALSE if it didn't
          if ( ! $deleted_from_db)
          {
             //set fail delete from db message
          }
          else
          {
              //deleted from both filesystem and DB, set success message
              $this->session->set_flashdata('success', '<p  red; font-weight:bold; font-size:20px;">Udalo sie usunac<p>');
          }
        }
        
      
        
        redirect('media_ctrl/media_view');
        }else{
        redirect('home/restricted');
        }
}
#5

[eluser]jonez[/eluser]
[quote author="rad11" date="1386963592"]
Code:
public function remove_file($id_image){
        
        $this->db->select('full_path');
        $this->db->where('id_image', $id_image);
        $query = $this->db->get('images');
        
        if ($query->num_rows() > 0)
        {
           $row = $query->row();
           echo $row->full_path;
        }
        unlink($row->full_path);

        
    }
[/quote]
Put the unlink in the if statement, if the record isn't found you'll get an error since row isn't defined. You shouldn't use echo use the output class: http://ellislab.com/codeigniter /user-guide/libraries/output.html

The output class adds to a buffer which allows you to do interesting things like automatically minify the content to reduce bandwidth (CI3 has this).
#6

[eluser]anthony_d_mays[/eluser]
It might be worth checking out CodeIgniter's File Helper. See File Helper in User Guide.




Theme © iAndrew 2016 - Forum software by © MyBB