PHP、thinkPHP5.0开发网站文件管理功能(三)编辑文件

public function edit(){
$file = iconv('UTF-8','GB2312',urldecode(input('file')));
if(empty($file)|| !file_exists($file)){
$this->error('操作异常');
}
$arr = ['.PHP', '.CSS', '.JS', '.XML', '.HTML', '.HTACCESS','.TXT','.JSON'];
$exc = strtoupper(substr($file,strrpos($file,'.')));
if (!in_array($exc, $arr)) {
$this->error("该文件类型不支持编辑");
}
if(request()->isPost()){
$content = input('code');
//打开要编辑的文件
$fp = fopen($file,'w');
//写入新内容
fwrite($fp,$content);
//关闭文件
fclose($fp);
$this->success('文件保存成功','file/index');
}
$code = htmlentities(file_get_contents($file),ENT_COMPAT,'UTF-8');//获取文件的内容,把HTML转为实体内容输出把代码展示在页面
$this->assign('code',$code);
$this->assign('currfile',$file);
$this->assign('ext', $exc);
return view("file_edit");
}
前端编辑界面推荐使用codemirror插件,能显示行数和高亮代码

猜你喜欢

转载自www.cnblogs.com/YAN-HUA/p/9083719.html