1. 程式人生 > >PHP、thinkPHP5.0開發網站文件管理功能(三)編輯文件

PHP、thinkPHP5.0開發網站文件管理功能(三)編輯文件

code AC RF ret 文件類型 功能 utf TP req

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開發網站文件管理功能(三)編輯文件