Removing directory using codeigniter

2019-01-27 18:25发布

问题:

I'm new to codeigniter so please forgive me, I am trying to make codeigniter delete a base folder (I don't know what the right term is, it's the folder where I put my uploaded files and its located in the root of my codeigniter project). Since I am new to codeigniter, I don't have any idea on how to make use of the File Helper reference in the codeigniter api. I hope someone can help me with this.

Furthermore, the delete function I am trying to make needs to delete also all of its contents, so empty or not, the folder gets deleted. I'm guessing, it would use a recursive type of deletion...I'm not so sure at all.

回答1:

You can delete all files using delete_files function

$path=$this->config->base_url().'dir_name';
$this->load->helper("file"); // load the helper
delete_files($path, true); // delete all files/folders

Above code will delete all files and folders from the given path and if once the directory is empty then you can use rmdir which deletes an empty directory, like

rmdir($path);

The folder should permit relevant permissions, which means files/folder must be writable or owned by the system in order to be deleted.



回答2:

You can do it using "delete_files" function:

$path = "the path that has files those will be deleted";
$this->load->helper("file"); // load codeigniter file helper
delete_files($path, true , false, 1); //  second and the last parameters are required, second parameter should be true and the last parameter should be greater than 0