首页 > Web开发 > 详细

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

时间:2018-05-24 17:24:21      阅读:267      评论:0      收藏: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插件,能显示行数和高亮代码

技术分享图片

 

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

原文:https://www.cnblogs.com/YAN-HUA/p/9083719.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!