CodeIgniter Forums
Could not delete a single image from a model!![SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Could not delete a single image from a model!![SOLVED] (/showthread.php?tid=10696)

Pages: 1 2


Could not delete a single image from a model!![SOLVED] - El Forum - 08-08-2008

[eluser]Mizanur Islam Laskar[/eluser]
Hi guys,I'm trying to delete a single file from a model. My partial model code is as below:

Code:
function delete_data($record_id)
            {
             $del_flag = 0;
            
             $this->db->delete('my_table', array('record_id' => $record_id));
             if( mysql_affected_rows() > 0 )
                 {
                    $query       =  $this->db->getwhere('my_table',array('record_id' => $record_id));
                    $row       = $query->row();
                    //$picture .= $row->picture;
                    @unlink(base_url()."assets/photo/".$picture);
                    $del_flag ++;
                 }
            
             return $del_flag;    
                    
             }

Note that, my data is deleting from my DB, but the image could not be deleted. I looked for the helper class called 'File Helper' to achieve this within the framework, but
Code:
delete_files('./path/to/directory/');
only for deleting all files within a folder. So, overall, I'm stuck in getting a way to delete a single image-file. Any help from any expert will be appreciated...thanks


Could not delete a single image from a model!![SOLVED] - El Forum - 08-08-2008

[eluser]ecsyle31[/eluser]
Uncomment the $picture variable and remove the @ to show the errors.


Could not delete a single image from a model!![SOLVED] - El Forum - 08-08-2008

[eluser]Mizanur Islam Laskar[/eluser]
I already did that, but not getting any good, neither any error message.


Could not delete a single image from a model!![SOLVED] - El Forum - 08-08-2008

[eluser]Colin Williams[/eluser]
base_url() will return the URI you set with $config['base_url']. There is very, very little chance that this is the appropriate relative path. Basically, unlink is getting passed a path it cannot resolve. You probably want to use APPPATH or FCPATH constants, which are system paths and not URIs.


Could not delete a single image from a model!![SOLVED] - El Forum - 08-08-2008

[eluser]Mizanur Islam Laskar[/eluser]
Thanks Colin. Can you plz give me an exaple code based on my model-code, since I'm not familier with "APPPATH or FCPATH".


Could not delete a single image from a model!![SOLVED] - El Forum - 08-08-2008

[eluser]ecsyle31[/eluser]
[quote author="Colin Williams" date="1218276953"]base_url() will return the URI you set with $config['base_url']. There is very, very little chance that this is the appropriate relative path. Basically, unlink is getting passed a path it cannot resolve. You probably want to use APPPATH or FCPATH constants, which are system paths and not URIs.[/quote]
Ah, yeah, good call Smile


Could not delete a single image from a model!![SOLVED] - El Forum - 08-09-2008

[eluser]SpooF[/eluser]
Code:
unlink(substr(FCPATH,0,-9)."assets/photo/".$picture);

FCPATH is the path your index.php including the index.php part. substr will remove the index.php part. This leaves you just the path to the index file and as long as your assets folder is in the same location as your index page you should be fine.


Could not delete a single image from a model!![SOLVED] - El Forum - 08-09-2008

[eluser]Référencement Google[/eluser]
You also can use the realpath() function, that's generally working for me.
Code:
unlink(realpath('assets/photo').'/'.$picture);



Could not delete a single image from a model!![SOLVED] - El Forum - 08-10-2008

[eluser]Mizanur Islam Laskar[/eluser]
Hello Too Pixel, I tried to use your code, but didn't work yet. My final partial model is:
Code:
function delete_data($record_id)
            {
             $del_flag = 0;
            
             $this->db->delete('projukti_committee', array('record_id' => $record_id));
             if( mysql_affected_rows() > 0 )
                 {
                    $query       =  $this->db->getwhere('projukti_committee',array('record_id' => $record_id));
                    $row       = $query->row();
                    $picture .= $row->picture;
                    @unlink(realpath('assets/photo').'/'.$picture);
                    $del_flag ++;
                 }
            
             return $del_flag;    
                    
             }



Could not delete a single image from a model!![SOLVED] - El Forum - 08-10-2008

[eluser]Référencement Google[/eluser]
Without sending us the error message you get (remove the @ before unlink) or a print_r of your variables it will be difficult to provide more help. Also you can try to enable CodeIgniter logging to see each process and check what are the problems.