CodeIgniter Forums
How to delete folder according to username. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to delete folder according to username. (/showthread.php?tid=71078)



How to delete folder according to username. - kirasiris - 07-03-2018

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.

This is the current path:

assets/img/users/avatars/brian
assets/img/users/covers/brian
assets/img/users/status/brian

the naming of the folder by username works great but when deleting the account instead of just deleting his/her folders(whether is within avatars/covers/status), it deletes the whole path, resulting on this(notice there are not avatar/covers/status folders now):

assets/img/users/

This is what I have in my model:
PHP Code:
   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;
 
       delete_files(FCPATH."assets/img/users/avatars/".$itemTRUE1);
 
       delete_files(FCPATH."assets/img/users/covers/".$itemTRUE1);
 
       delete_files(FCPATH."assets/img/users/status/".$itemTRUE1);
 
       rmdir("assets/img/users/avatars/".$item);
 
       rmdir("assets/img/users/covers/".$item);
 
       rmdir("assets/img/users/status/".$item);
 
   

I hope someone can  help me.

Thanks in advance.


RE: How to delete folder according to username. - jreklund - 07-03-2018

It looks like $item dosen't contain anything.
You are using delete() before getting the username.

You need to check so that $item aren't NULL and that those folders exists, before deleting them.


RE: How to delete folder according to username. - kirasiris - 07-03-2018

(07-03-2018, 09:54 AM)jreklund Wrote: It looks like $item dosen't contain anything.
You are using delete() before getting the username.

You need to check so that $item aren't NULL and that those folders exists, before deleting them.

OMG! Dude, I've been trying to figure out this the whole day and I never though of just putting the delete in he very end because I though it really did not matter bacause I wanted to delete a folder containing the username not the ID but hey, thanks it worked!!. Always learning!.


RE: How to delete folder according to username. - InsiteFX - 07-04-2018

Looks like he is deleting the table before getting the username.