Welcome Guest, Not a member yet? Register   Sign In
How to delete folder according to username.
#1

(This post was last modified: 07-03-2018, 07:17 AM by kirasiris.)

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.
I do Front-End development most of the time 
Reply
#2

(This post was last modified: 07-03-2018, 09:55 AM by jreklund.)

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.
Reply
#3

(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!.
I do Front-End development most of the time 
Reply
#4

Looks like he is deleting the table before getting the username.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB