Welcome Guest, Not a member yet? Register   Sign In
Could not delete a single image from a model!![SOLVED]
#1

[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
#2

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

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

[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.
#5

[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".
#6

[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
#7

[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.
#8

[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);
#9

[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;    
                    
             }
#10

[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.




Theme © iAndrew 2016 - Forum software by © MyBB