在程序./Application/Common/Conf目录下新建一个System.php文件,用于存放数据,并在config.php文件中调用该文件配置
//扩展配置
'LOAD_EXT_CONFIG' => 'system',
新建一个控制器
<?php
namespace Tdcq\Controller;
use Think\Controller;
class SystemController extends BaseController {
public function index(){
if(IS_POST){
$file='./Etc/Common/Conf/system.php';
$pic=array();
//上传图片
if($_FILES != ''){
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 0 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg','bmp');// 设置附件上传类型
$upload->rootPath='./';
$upload->savePath = 'Public/Logo/'; // 设置附件上传目录
$info=$upload->upload($_FILES);
foreach ($info as $k => $v) {
$pic[$k]=$v['savepath'].$v['savename'];
}
}
$post=$_POST;
$post=array_merge($pic,$post);
$post=array_change_key_case($post,CASE_UPPER);
$config=array_merge(include $file,$post);
$config=var_export($config,true);
$str="<?php\r\nreturn ".$config.";";
if(file_put_contents($file,$str)){
$this->success('修改成功');
}else{
$this->error('修改失败');
}
return;
}
$this->display();
}
public function del(){
$pic1=I('path.2');
$pic2=I('path.3');
$pic3=I('path.4');
$pic4=I('path.5');
$pic=$pic1.'/'.$pic2.'/'.$pic3.'/'.$pic4;
$pic=str_replace("'","",$pic);
$file='./Etc/Common/Conf/system.php';
if($pic){
if(unlink($pic)){
//读取文件
$origin_str = file_get_contents($file);
//修改内容
$update_str = str_replace($pic, '', file_get_contents($file));
//写入保存
file_put_contents($file, $update_str);
$this->success("删除图片成功");
}else{
$this->error('删除图片失败');
}
}
}
}
<label class="col-sm-2 control-label">LOGO</label>
<div class="col-sm-10">
<if condition="$Think.config.LOGO neq ''">
<img src="__ROOT__/{$Think.config.LOGO}" height="50">
<a href="__CONTROLLER__/del/'{$Think.config.LOGO}'" />删除</a>
<else/>
<input name="logo" type="file" class="form-control" >
</if>
</div>
</div>