项目是thinkphp5的,后台有一个,上传html模板代码的功能。

因为模板里,除了html代码外,还有,

{volistname="list"id="vo"}{/volist}{$title}

php,js代码。

我是这样做的,先建一个template表。

CREATETABLEhk_template(idINT(10)PRIMARYKEYNOTNULL,nameVARCHAR(30),contentTEXTCOMMENT'markdown代码',content_htmlTEXTCOMMENT'html代码',filepathVARCHAR(255)COMMENT'所在位置,比如:application/index/view/special/detail.html',statusTINYINT(1)DEFAULT'1'COMMENT'1启用,0不启用',create_timeINT(10),update_timeINT(10),create_uidINT(10),update_uidINT(10));


写入,用textarea框,将,代码全部写入到content中。

在写入成功后,再将content,的内容,

放在到模板文件中。

因为在/public/index.php中有定义APP_PATH,

//[应用入口文件]//定义应用目录define('APP_PATH',__DIR__.'/../application/');define('WEB_URL','http://'.$_SERVER['HTTP_HOST']);//加载框架引导文件require__DIR__.'/../thinkphp/start.php';

.edit方法如下:

publicfunctionedit($id){$template=model('Template');if(IS_POST){$data=$_POST;//提交表單$result=$template->allowField(true)->save($data,['id'=>$data['id']]);if($result){//目标文件:application/index/view/special/detail.html,替换里面的内容。//$filename=APP_PATH.'index/view/specail/detail.html';//chmod($filename,777);//写这一句无权限修改,就只好在目录下手动修改了$file=fopen($filename,"w");//以写模式打开文件fwrite($file,$data['content']);//写入第一行fclose($file);//关闭文件return$this->success("更新成功!",url('admin/template/index'));}else{return$this->error($template->getError(),url('admin/template/add'));}}else{//單條記錄if(!$id){$this->error('非法操作');}$info=$template->where('id',$id)->find();if(!$info){return$this->error($template->getError());}$this->assign('info',$info);$this->setMeta('模板編輯');return$this->fetch();}}



下面是转载自:

http://www.jb51.net/article/68202.htm


本文实例讲述了php写入、删除与复制文件的方法。分享给大家供大家参考。具体如下:


写入:


<?php $filename = "Test//file.txt"; $file = fopen($filename, "w"); //以写模式打开文件 fwrite($file, "Hello, world!/n"); //写入第一行 fwrite($file, "This is a test!/n"); //写入第二行 fclose($file); //关闭文件 ?>


2. 删除:


<?php $filename = "Test//file.txt"; unlink($filename); //删除文件 ?>


3.复制:


<?php $filename1 = "Test//file.txt"; $filename2 = "Test//file.bak"; copy($filename1, $filename2); //复制文件 ?>

希望本文所述对大家的php程序设计有所帮助。