我使用Codeigniter 3x,我想在我的Codeigniter项目中创建CMS的某些部分,例如从CSS/JS/ header_view.php
, footer_view.php
读取/加载内容文件并保存它,以便它像wordpress编辑器一样覆盖该文件。
步骤如下:
- 显示文件列表给用户
- 允许他们点击一个文件
- 加载文件内容并在视图或所见即所得编辑器中打印
- 当他们保存时,获取整个内容并覆盖旧内容。
显示文件列表,只需使用:
$dir = 'path/to/your/css/files'
$files = glob($dir.'*');
现在,在你的视图中,为每个文件创建一个链接到接受文件名的控制器方法,例如:
foreach($files as $file){
<li><a href="path/to/your/controller/method/".$file>$file</a></li>
}
这样你就有了指向文件的链接,所以你可以重新创建你应该知道的完整路径(如果你想,你可以用一些data-
属性扩展链接,使它更容易)。
现在你在控制器方法中,所以只需加载文件内容并将其发送给视图:
$file_name = ''; // the one you get from the method link
$content = read_file($file_name);
// load the view and the editor
现在允许用户进行他/她想要的所有更改,然后保存数据:
$file_name = ""; // Name of the file, you can get it from the url, or adding as input in the form
$file_content = html_entity_decode($this->input->post('file_content'));
write_file($file_name , $file_content)
一定要重新创建文件的完整路径