Deleting a File using php/codeigniter

前端 未结 9 1884
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 07:47

I would like to delete a file that is found in my localhost.

localhost/project/folder/file_to_delete

I\'m using codeigniter for this.

9条回答
  •  一整个雨季
    2021-01-04 08:22

    This code can also handle non-empty folders - just use it in a helper.

    if (!function_exists('deleteDirectory')) {
        function deleteDirectory($dir) {
        if (!file_exists($dir)) return true;
        if (!is_dir($dir) || is_link($dir)) return unlink($dir);
            foreach (scandir($dir) as $item) {
                if ($item == '.' || $item == '..') continue;
                if (!deleteDirectory($dir . "/" . $item)) {
                    chmod($dir . "/" . $item, 0777);
                    if (!deleteDirectory($dir . "/" . $item)) return false;
                };
            }
            return rmdir($dir);
        }
    }
    

提交回复
热议问题