phpdirectorycodeigniter-3rmdir

Deleting folder in CodeIgniter


UPDATE: I already solved, I dont know how but it is now working, here it is the new update function in case any future users find themselves in a similar problem.

Assuming that the $id var comes from a function that fetch the current id of the data being deleted and assuming you already have a function which creates folders according to usernames, the following should work!.

public function delete($id)
    {
        $this->db->where('id', $id);
        $this->db->delete($this->table);


        // If users closes his account, all of his folders should be deleted as well
        $item = $this->get($id)->username;
        //varDebug($item);
        delete_files(FCPATH."assets/img/users/avatars/".$item, TRUE);
        delete_files(FCPATH."assets/img/users/covers/".$item, TRUE);
        delete_files(FCPATH."assets/img/users/status/".$item, TRUE);
        rmdir("assets/img/users/avatars/".$item);
        rmdir("assets/img/users/covers/".$item);
        rmdir("assets/img/users/status/".$item);
    }

I have a register form in which users can registers to create their own account in my application and I'm giving them the oportunity to upload their own pictures etc. Everything works well so far.

The problem comes when the user and/or admin deletes an user account. For example I as an admin, I decide to delete Brian's account, all of his folders should be deleted as well.

The current output is tht it deletes everyone(if there are more than his own account) folders instead of his own account.

So here is what I have in my model:

public function delete($id)
    {
        $this->db->where('id', $id);
        $this->db->delete($this->table);


        // If users closes his account, all of his folders should be deleted as well
        $item = $this->get($id);
        delete_files("assets/img/users/avatars/".$item['username']."/", TRUE);
        delete_files("assets/img/users/covers/".$item['username']."/", TRUE);
        delete_files("assets/img/users/status/".$item['username']."/", TRUE);
        rmdir("assets/img/users/avatars/".$item['username']."/");
        rmdir("assets/img/users/covers/".$item['username']."/");
        rmdir("assets/img/users/status/".$item['username']."/");
    }

Can someone help me with this?


Solution

  • You need to provide a complete path to delete_files(). Easily done. Simply add FCPATH to the beginning of the paths. That assumes that assets is on the same level as application and system. Adjust accordingly.

    FCPATH is the absolute path to the same folder that index.php is in.

    delete_files(FCPATH."assets/img/users/avatars/".$item['username'], TRUE);
    

    You do not need the trailing slash on the path. No harm if you do, but delete_files() will strip it off anyway.