CodeIgniter Forums
unlink image from directory - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: unlink image from directory (/showthread.php?tid=52076)



unlink image from directory - El Forum - 05-29-2012

[eluser]meer[/eluser]
hi to all, im passing image id from my view , getting that id from controller by uri segment,now iwanna delete data from db and image from its directory,
fields are deleting fine , but image is still residing in that directory
can i done this with same query from the model?

im using following codes

view
<a href="&lt;?php echo base_url(); ?&gt;add_user_controller/delete/&lt;?php echo $row-&gt;id; ?&gt;">

controller

$id=$this->uri->segment(3);
$this->load->model('users_model');
$this->users_model->delete($id);

model

function delete($id)
{
$this->db->where('id',$id);
$this->db->delete('register');
}

what kind of query i needed to delete data from table and image from image folder?

thanks in advance



unlink image from directory - El Forum - 05-29-2012

[eluser]Pedro Luz[/eluser]
you can delete the image, but you shouldn't not do that from the model.
its beter to return true or false, from the delete function in the model.. if the record has been deleted... and in the controller use the function unlink

Code:
function delete($id)
{
$this->db->where('id', $id);
$result = $this->db->delete('mytable');

if($result) return true;

return false;
}

in the controller:

Code:
if($this->user_model->delete(5))
{
unlink($filename)
}

to delete the image you just do

Code:
unlink($filename);

take a look at
http://pt2.php.net/manual/en/function.unlink.php


unlink image from directory - El Forum - 05-29-2012

[eluser]meer[/eluser]
ok , but how do i get the filename from the same query?




unlink image from directory - El Forum - 05-29-2012

[eluser]Pedro Luz[/eluser]
depends what's the relation betwwen the user and that image?

do you have the image filename in a column in a table?

http://stackoverflow.com/questions/3892357/php-remove-image-from-server


unlink image from directory - El Forum - 05-29-2012

[eluser]meer[/eluser]
yup, i have got that image name from an other query , but i wanna do this with the same query from the model........



unlink image from directory - El Forum - 05-29-2012

[eluser]Pedro Luz[/eluser]
can you post the other code here? from the other query?


unlink image from directory - El Forum - 05-29-2012

[eluser]meer[/eluser]
controller
function delete()
{
$id=$this->uri->segment(3);
$this->db->select('image');
$this->db->from('register');
$this->db->where('id',$id);
$query = $this->db->get();
foreach($query->result() as $row)
{
$path=$row->image;
unlink("uploads/".$path);
$this->load->model('users_model');
$this->users_model->delete($id);
}
}
i wanna do this all in model with one querry