Welcome Guest, Not a member yet? Register   Sign In
unlink photo
#1

[eluser]Thiago Leao[/eluser]
I can delete the path from the bank, but I can not delete the files.

Well I know I have to return an object to delete, but I can not, can someone help me?
thanks

Code:
function removePhoto($id_pictures){
        
        $sql = $this->db->where('id_pictures', $id_pictures);
        $this->db->delete('pictures');
        
        $query = $this->db->query($sql);
        return $query->result();
        
        unlink(base_url().$query->thumb);
        unlink(base_url().$query->imagem);
        
    }
#2

[eluser]Narkboy[/eluser]
Would it not be better to unlink before deleteing the db record?

It looks like you're deleteing the db record, then querying for that same record to get the thum and image filenames, which are passed to unlink.

Try:
Code:
function removePhoto($id_pictures){
        
        $query = $this->db->where('id_pictures', $id_pictures);
        $result = $query->result();
        
        unlink(base_url().$result->thumb);
        unlink(base_url().$result->imagem);
        
        $this->db->delete('pictures');
    }

Don't use that code directly - you'll need to check num_rows() to ensure you have a result, and you're also not specifying which record to get the thumb and imagem from. IIRC ->result() gives you an array of objects? So you;ll want $result[0]->thumb etc?

/B
#3

[eluser]Thiago Leao[/eluser]
Code:
Fatal error: Call to undefined method CI_DB_mysql_driver::result() in /home/thiagoleao/public_html/sites/catalogo/system/application/models/administracao/cad_produto_model.php on line 53

Line 53: $result = $query->result();
#4

[eluser]Narkboy[/eluser]
Ok - I haclked a little because I never get results as objects. Always seems to work faster with arrays unless you need the oop.

So:

Code:
function remove_photo($photo_id) {
    $this->db->where('id_pictures',$photo_id);
    $query = $this->db->get('pictures_table_name');

    if ( $query->num_rows() != 1) {
        // No record with this id:
        return FALSE;
    }

    $result = $query->result_array();
    $result = $result[0]; // Remove the extra level; we don't need it.

    // Now unlink as required:
    unlink( base_url() . $result['thumb'] );
    unlink( base_url() . $result['imagem'] );

    // Now delete from the db:
    $this->db->delete('table_name',array('id_pictures' => $photo_id);

    // Now you're done, either confirm or just trust:
    return TRUE;
}

Is how I'd do it.
#5

[eluser]Thiago Leao[/eluser]
thank you friend.
perfect!

I just had to change those lines:

Code:
unlink( base_url() . $result['thumb'] );
unlink( base_url() . $result['imagem'] );

to

unlink( realpath($result['thumb']) );
unlink( realpath($result['imagem']) );
#6

[eluser]Narkboy[/eluser]
Smile
#7

[eluser]Thiago Leao[/eluser]
my friend, can I ask one more help?
when I delete the image, I would like to return to the same id.

Example: I'm excluding the product image id 8.

if it were on hand, would be:

Code:
upload_produto.php? id_products = $ _GET ['id_products'];

How to do it in the CI, so I'm trying:

VIEW

Code:
<th width="28">
<a >id_pictures; ?&gt;/&lt;?php echo $item->id_products; ?&gt;" onClick="return(deleta());" title="Apagar">
&lt;?php echo img(array('src' => 'assets/admin/images/apagar.png', 'alt' => 'Apagar'));?&gt;</a>
</th>

CONTROLLER

Code:
function upload($id_products){
        
        $param['titulo'] = 'Catalogo Colletion - Administração!';    
    
        $this->load->model('administracao/cad_produto_model');
        
        $param['dados_produto'] = $this->cad_produto_model->alterar($id_products);
        $param['produtos']         = $this->cad_produto_model->listar_img($id_products);
    
        $this->load->view('administracao/upload_produto', $param);
        
    }
    
    function removePhoto($id_pictures){
        $this->load->model('administracao/cad_produto_model');    
        $this->cad_produto_model->removePhoto($id_pictures);
        
        $data['id_products']     = $this->input->get('id_products');
        redirect(base_url().'administracao/cad_produto/upload/'.$data['id_products'], 'refresh');
    }
#8

[eluser]Narkboy[/eluser]
Ok - so you want to remove an image, and then reuse the same id?

So - delete image id 8, then be redirected to the upload page so that you can upload a new image with id 8?

It's do-able, but the big question is why would you?

If you store images in the database, then you can manually specify the id field:

Code:
$insert = array(
    'image_id' => '8',
    'image_filename' => 'file.jpg'
};

$this->db->insert('table_name',$insert);

I assume that the field for id's in the db is autoinc? In which case, you shouldn't get an error if you specify the id.

However, I don't see a reason why you would need or want to. You'd require two insert functions - one for specifying the id, and one for when you don't want to specify.

Why not just allow the database to sort the ids for you?

/B
#9

[eluser]Thiago Leao[/eluser]
i think I did not understand!

I have 2 tables, pictures and products.
I have a product with id 8, I upload pictures on the table and record linking that product id.

example:

Table pictures
- Id_pictures (1)
- Id_products (8)
- Thumb (image.jpg)
- Image (image.jpg)

when I delete the images, I want to return to id_products I was uploading!

you understand?
thanks friend!
#10

[eluser]Narkboy[/eluser]
Ahh ok I see - that makes much more sense!!

In that case, your controllers would seem to have this ability already. All you need to do is pass the producty_id to the upload controller, and have it added to the view form as a hidden input.

if:

Code:
administracao/cad_produto/upload/[product_id]

shows a form that will upload correctly, then the:

Code:
redirect(base_url().'administracao/cad_produto/upload/'.$data['id_products'], 'refresh');

should work fine.

Side note, try to use 'location' rather than 'refresh', though 'location' sometimes causes problems on Windows.

Where do you go and what does not work when you run this script?

/B




Theme © iAndrew 2016 - Forum software by © MyBB