Deleting folders and their content. - 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: Deleting folders and their content. (/showthread.php?tid=67891) |
Deleting folders and their content. - keithmclaughlin - 04-20-2017 Hi, I'm having problems when it comes to deleting specific folders. All folders get deleted inside an "uploads" folder when I only want a single folder deleted. My application allows new companies to register on the system. When a company registers a new folder gets created inside the uploads folder specific to that company (a random md5 string is used). My folder setup: - public_html / htdocs - uploads - 731e315adc887be41cc84be0f5c73f76 - avatars - b8218d22eaaca5deb91c741c4d7112dd - 71913f59e458e026d6609cdb5a7cc53d - etc... In the app I provide admins the ability to delete companies. When a company is being deleted I want all files & subfolders inside the company specific folder deleted and then the folder itself. The md5 string is stored in a MySQL database (as 'uploads_folder') along with other info about the company. In the delete function in the Companies class I get the company info using the following: PHP Code: $company_folder = $this->company_lib->get_uploads_folder($id); The get_uploads_folder function inside the Company_lib library simply returns the full path: PHP Code: public function get_uploads_folder($company_id) I then load the file helper and call the delete_files function: PHP Code: $this->load->helper('file'); When the delete_files function runs it deletes all folders inside the uploads folder instead of just deleting the one specific to that company. What am I doing wrong? By the way this is happening when using WAMP on my windows computer. I haven't tested it on my host yet (which uses Unix/Linux) for fear of deleting even more folders! RE: Deleting folders and their content. - dave friend - 04-20-2017 Have you checked that $company_folder is what you expect it to be? RE: Deleting folders and their content. - Rufnex - 04-20-2017 After your method call in $company_folder you will have a string like that: public_html/htdocs/uploads/731e315adc887be41cc84be0f5c73f76 This directory should be deleted with all content ... can you post yourdelete_files method. Your problem should be there ... RE: Deleting folders and their content. - dave friend - 04-20-2017 @Rufnex, he is using the delete_files function from CI's File Helper My guess is that the model is returning public_html/htdocs/uploads and not public_html/htdocs/uploads/731e315adc887be41cc84be0f5c73f76 RE: Deleting folders and their content. - Rufnex - 04-20-2017 @Dave friend: jup, you're right. simple print out the return value to check if the path it correct. RE: Deleting folders and their content. - keithmclaughlin - 04-21-2017 The $company_folder does have the path to the correct folder to delete. As I said I'm using WAMP so this is the path I get when testing it: C:\wamp64\www\foresight\public\uploads\731e315adc887be41cc84be0f5c73f76 RE: Deleting folders and their content. - Rufnex - 04-21-2017 Try to add a / to the end of your path: C:\wamp64\www\foresight\public\uploads\731e315adc887be41cc84be0f5c73f76\ RE: Deleting folders and their content. - keithmclaughlin - 04-22-2017 (04-21-2017, 01:55 PM)Rufnex Wrote: Try to add a / to the end of your path: I did already and it made no difference. The delete_files function does this anyway. RE: Deleting folders and their content. - Rufnex - 04-23-2017 Try to remove the folder and content with nativ php .. if there is the same behavior you must have a config, logic or error. For e.g. like that: Code: function recursiveRmDir($dir) RE: Deleting folders and their content. - keithmclaughlin - 04-24-2017 (04-23-2017, 10:39 AM)Rufnex Wrote: Try to remove the folder and content with nativ php .. if there is the same behavior you must have a config, logic or error. For e.g. like that: Same result. All folders inside the uploads folder got deleted. It must be a windows thing. I'm going to try it on my hosts server. |