• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deleting folders and their content.

#1
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)
{
 
   $company $this->CI->company_model->fields('uploads_folder')->get($company_id);

 
   return FCPATH 'uploads'DIRECTORY_SEPARATOR $company['uploads_folder'];


I then load the file helper and call the delete_files function:
PHP Code:
$this->load->helper('file');

// delete all uploaded files for the company
delete_files($company_folderTRUE); 

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!
Reply

#2
Have you checked that $company_folder is what you expect it to be?
Reply

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

Reply

#4
@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
Reply

#5
@dave friend: jup, you're right.

simple print out the return value to check if the path it correct.

Reply

#6
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
Reply

#7
Try to add a / to the end of your path:

C:\wamp64\www\foresight\public\uploads\731e315adc887be41cc84be0f5c73f76\

Reply

#8
(04-21-2017, 02:55 PM)Rufnex Wrote: Try to add a / to the end of your path:

C:\wamp64\www\foresight\public\uploads\731e315adc887be41cc84be0f5c73f76\

I did already and it made no difference. The delete_files function does this anyway.
Reply

#9
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)
{
    $iterator = new RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
    foreach ($iterator as $filename => $fileInfo) {
        if ($fileInfo->isDir()) {
            rmdir($filename);
        } else {
            unlink($filename);
        }
    }
}

Reply

#10
(04-23-2017, 11: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:

Code:
function recursiveRmDir($dir)
{
   $iterator = new RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
   foreach ($iterator as $filename => $fileInfo) {
       if ($fileInfo->isDir()) {
           rmdir($filename);
       } else {
           unlink($filename);
       }
   }
}

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
2 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.