Welcome Guest, Not a member yet? Register   Sign In
delete file and DB entry
#1

[eluser]Dregond Rahl[/eluser]
Code:
function delete_file($user, $delete_key)
    {
        $data = array();
            
        $this->db->where('user_name', $user);
        
        $this->db->where('delete_key', $delete_key);
        
        $query = $this->db->get('ed_files');

        if ($query->num_rows() > 0)
        {
            $data = $query->result_array();
        }
        
        if (unlink($data[0]['full_path']))
        {
            $this->db->where('user_name', $user);
            
            $this->db->where('delete_key', $delete_key);
            
            $this->db->delete('ed_files');
        }
        
        $query->free_result();
    }


There has got to be a better way of doing this and handling multiple files deletion. Anyone has any ideas? I greatly appreciate it
#2

[eluser]drewbee[/eluser]
I don't get what your problem is? Do you want a more efficient way of doing this? Because of the way you have this setup, you need to know the full path that is stored in the row. The only other way you could do this is to actually pass the 'full path' to the function itself, and you could get rid of an extra query.

Code:
function delete_file($user, $delete_key, $full_path)
{
    $this->db->where('user_name', $user);
        
    $this->db->where('delete_key', $delete_key);
        
    $query = $this->db->delete('ed_files');

    if ($this->db->affected_rows() > 0)
    {
         @unlink($full_path);
    }

}
#3

[eluser]Jondolar[/eluser]
Even if your unlink fails, you might still want to remove the database record. You don't know if it failed because the file is missing or because of permissions or any other reason. You might want to log the fact that the delete failed and still delete the database record.

If you are concerned about the number of lines required, you could just write your own query:
mysql_query('delete from ed_files xxx');

You can also use Active Record caching (see the bottom of this page)
http://ellislab.com/codeigniter/user-gui...ecord.html
That should save you a few lines.
#4

[eluser]Dregond Rahl[/eluser]
Thanks both of you, i was just looking for alternative ways because i didnt like sending two requests, the method drewbee stated would work best, then even if the fullpath changes i can control it better

Again thank you
#5

[eluser]quest13[/eluser]
Hi,

I tried the same and got the following error.

Any idea of what went wrong ?


A PHP Error was encountered
Severity: Warning

Message: unlink() [function.unlink]: http does not allow unlinking

Filename: models/my_model.php

Line Number: 1012




Theme © iAndrew 2016 - Forum software by © MyBB