TP5项目中模板文件html的更新及php写入、删除与复制文件的方法

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

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

1
2
3
{volist name= "list"  id= "vo" }{/volist}
 
{ $title }
1
php,js代码。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE TABLE hk_template
(
     id INT(10) PRIMARY KEY NOT NULL,
     name VARCHAR(30),
     content TEXT COMMENT  'markdown代码' ,
     content_html TEXT COMMENT  'html代码' ,
     filepath VARCHAR(255) COMMENT  '所在位置,比如:application/index/view/special/detail.html' ,
     status TINYINT(1) DEFAULT  '1'  COMMENT  '1启用,0不启用' ,
     create_time INT(10),
     update_time INT(10),
     create_uid INT(10),
     update_uid INT(10)
);


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

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

放在到模板文件中。

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

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

.edit方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public  function  edit( $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写入、删除与复制文件的方法。分享给大家供大家参考。具体如下:


  1. 写入:


<?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程序设计有所帮助。



     本文转自phpervip 51CTO博客,原文链接:http://blog.51cto.com/phpervip/1933026,如需转载请自行联系原作者



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值