Rumah > php教程 > PHP源码 > php使用COPY函数更新配置文件的方法_php技巧

php使用COPY函数更新配置文件的方法_php技巧

PHP中文网
Lepaskan: 2016-05-25 17:15:13
asal
1211 orang telah melayarinya

这篇文章主要介绍了php使用COPY函数更新配置文件的方法,涉及copy函数更新配置信息的相关技巧,需要的朋友可以参考下

本文实例讲述了php使用COPY函数更新配置文件的方法。分享给大家供大家参考。具体如下:

_saveconfig.php文件如下:

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

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

<?php

/*

 * File: _saveconfig.php

 *

 * Modified: 2010-7-11

 * By:liulang

 * 说明:涉及到两个文件:_saveconfig.php和config.php当对后台的一些全局配置变量进行更新时,

 * 就更改后台的config.php然后通过这个文件更改前台的config.php文件(前后台文件都一样),

 * 这个是我曾经用过的一个方法,觉得还不错,拿出来与大家分享

 *

 * Created: 2010-6-20

 * By: liulang (xujiaphp@gmail.com)

 *

 */

  $srcFile = &#39;../&#39;.$dRootDir.&#39;#data/config.php&#39;;

  $dstFile = $dRootDir.&#39;config.php&#39;;

  $content = file_get_contents($srcFile);

  $arr = array

  (

     array(&#39;SITENAME&#39;, $gSite[&#39;siteName&#39;]),

     array(&#39;SITENAMEEN&#39;, $gSite[&#39;siteNameEn&#39;]),

     array(&#39;SITEKEYWORDS&#39;, $gSite[&#39;siteKeywords&#39;]),

     array(&#39;SITEDESCRIPTION&#39;, $gSite[&#39;siteDescription&#39;]),

     array(&#39;URLPREFIX&#39;, $gUrlPrefix),

     array(&#39;DBHOST&#39;, $gDb[&#39;host&#39;]),

     array(&#39;DBUSER&#39;, $gDb[&#39;user&#39;]),

     array(&#39;DBPWD&#39;, $gDb[&#39;pwd&#39;]),

     array(&#39;DBNAME&#39;, $gDb[&#39;db&#39;]),

     array(&#39;DBPREFIX&#39;, $gDb[&#39;prefix&#39;]),

     array(&#39;DIR&#39;, $gUpload[&#39;dir&#39;]),

     array(&#39;IMAGEWIDTH&#39;, $gUpload[&#39;imageWidth&#39;]),

     array(&#39;IMAGEHEIGHT&#39;, $gUpload[&#39;imageHeight&#39;]),

     array(&#39;CONTACTUS&#39;, $contactus),

     array(&#39;MENUHIDDENp&#39;, $menuhiddenp),

     array(&#39;THEME&#39;, $gTheme),

     array(&#39;DATE&#39;, date(&#39;Y-m-d&#39;)),

     array(&#39;USER&#39;, $dAdminName)

   );

   for ($i = 0; $i < count($arr); $i++)

   {

    $content = str_replace(&#39;~`~&#39;.$arr[$i][0].&#39;~`~&#39;, $arr[$i][1], $content);

   }

   copy($dRootDir.&#39;config.php&#39;, $dRootDir.&#39;bak.config.php&#39;);

   //copy($dRootDir.&#39;bak.config.php&#39;, &#39;../&#39;.$dRootDir.&#39;config.php&#39;);

   $done = file_put_contents($dstFile, $content);

   copy($dstFile, &#39;../&#39;.$dRootDir.&#39;config.php&#39;);

?>

Salin selepas log masuk

config.php配置文件如下:

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

33

34

35

36

37

38

39

40

41

<?php

/*

 * File: config.php

 *

 * Modified: 2010-09-20

 * By:admin

 *

 * Created: 2010-6-20

 * By: liulang (xujiaphp@gmail.com)

 *

 */

  $gVersion = &#39;1.0&#39;;

  $gDb = array

  (

    &#39;host&#39; => &#39;localhost&#39;,   //主机名

    &#39;user&#39; => &#39;root&#39;,   //用户名

    &#39;pwd&#39; => &#39;&#39;,    //密码

    &#39;db&#39; => &#39;sino&#39;,    //数据库名

    &#39;prefix&#39; => &#39;sin_&#39;  //数据库前缀

  );

  $gSite = array

  (

    &#39;siteName&#39; => &#39;公司名&#39;,  

    &#39;siteNameEn&#39; => &#39;we are the company&#39;, 

    &#39;siteKeywords&#39; => &#39;药物,医药,制药&#39;, 

    &#39;siteDescription&#39; => &#39;公司名是一家从事00方面的公司&#39;

  );

  $gUpload = array

  (

    &#39;dir&#39; => &#39;uploads&#39;,

    &#39;imageWidth&#39; => &#39;120&#39;,

    &#39;imageHeight&#39; => &#39;*&#39; 

  );

  $contactus = &#39;&#39;;

  $menuhiddenp = "";

  $FROMURL=$_SERVER["HTTP_REFERER"]?$_SERVER["HTTP_REFERER"]:$HTTP_SERVER_VARS["HTTP_REFERER"];

  $dRootDir = &#39;../&#39;;

  $conn = mysql_connect($gDb[&#39;host&#39;],$gDb[&#39;user&#39;],$gDb[&#39;pwd&#39;]);

  mysql_select_db($gDb[&#39;db&#39;]) or die(&#39;database connect error!&#39;);

  mysql_query("SET NAMES &#39;gbk&#39;");

?>

Salin selepas log masuk

Label berkaitan:
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Isu terkini
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan