首页 > 开发 > PHP > 正文

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

2024-05-04 23:36:26
字体:大 中 小
来源:转载
供稿:网友

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

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

_saveconfig.php文件如下:

 

 
  1. <?php 
  2. /* 
  3. * File: _saveconfig.php 
  4. *  
  5. * Modified: 2010-7-11 
  6. * By:liulang 
  7. * 说明:涉及到两个文件:_saveconfig.php和config.php当对后台的一些全局配置变量进行更新时, 
  8. * 就更改后台的config.php然后通过这个文件更改前台的config.php文件(前后台文件都一样), 
  9. * 这个是我曾经用过的一个方法,觉得还不错,拿出来与大家分享 
  10. * 
  11. * Created: 2010-6-20 
  12. * By: liulang (xujiaphp@gmail.com) 
  13. *  
  14. */ 
  15. $srcFile = '../'.$dRootDir.'#data/config.php'; 
  16. $dstFile = $dRootDir.'config.php';  
  17. $content = file_get_contents($srcFile); 
  18. $arr = array 
  19. ( 
  20. array('SITENAME', $gSite['siteName']), 
  21. array('SITENAMEEN', $gSite['siteNameEn']), 
  22. array('SITEKEYWORDS', $gSite['siteKeywords']), 
  23. array('SITEDESCRIPTION', $gSite['siteDescription']), 
  24. array('URLPREFIX', $gUrlPrefix), 
  25. array('DBHOST', $gDb['host']), 
  26. array('DBUSER', $gDb['user']), 
  27. array('DBPWD', $gDb['pwd']), 
  28. array('DBNAME', $gDb['db']), 
  29. array('DBPREFIX', $gDb['prefix']), 
  30. array('DIR', $gUpload['dir']), 
  31. array('IMAGEWIDTH', $gUpload['imageWidth']), 
  32. array('IMAGEHEIGHT', $gUpload['imageHeight']), 
  33. array('CONTACTUS', $contactus), 
  34. array('MENUHIDDENDIV', $menuhiddendiv), 
  35. array('THEME', $gTheme), 
  36. array('DATE', date('Y-m-d')), 
  37. array('USER', $dAdminName) 
  38. ); 
  39. for ($i = 0; $i < count($arr); $i++) 
  40. { 
  41. $content = str_replace('~`~'.$arr[$i][0].'~`~', $arr[$i][1], $content); 
  42. }  
  43. copy($dRootDir.'config.php', $dRootDir.'bak.config.php'); 
  44. //copy($dRootDir.'bak.config.php', '../'.$dRootDir.'config.php'); 
  45. $done = file_put_contents($dstFile, $content); 
  46. copy($dstFile, '../'.$dRootDir.'config.php'); 
  47. ?> 

config.php配置文件如下:

 

 
  1. <?php 
  2. /* 
  3. * File: config.php 
  4. *  
  5. * Modified: 2010-09-20 
  6. * By:admin 
  7. *  
  8. * Created: 2010-6-20 
  9. * By: liulang (xujiaphp@gmail.com) 
  10. *  
  11. */ 
  12. $gVersion = '1.0'; 
  13. $gDb = array 
  14. ( 
  15. 'host' => 'localhost', //主机名 
  16. 'user' => 'root', //用户名 
  17. 'pwd' => '', //密码 
  18. 'db' => 'sino', //数据库名 
  19. 'prefix' => 'sin_' //数据库前缀 
  20. ); 
  21. $gSite = array 
  22. ( 
  23. 'siteName' => '公司名',  
  24. 'siteNameEn' => 'we are the company',  
  25. 'siteKeywords' => '药物,医药,制药',  
  26. 'siteDescription' => '公司名是一家从事00方面的公司' 
  27. ); 
  28. $gUpload = array 
  29. ( 
  30. 'dir' => 'uploads', 
  31. 'imageWidth' => '120', 
  32. 'imageHeight' => '*' 
  33. ); 
  34. $contactus = ''; 
  35. $menuhiddendiv = ""; 
  36. $FROMURL=$_SERVER["HTTP_REFERER"]?$_SERVER["HTTP_REFERER"]:$HTTP_SERVER_VARS["HTTP_REFERER"]; 
  37. $dRootDir = '../'; 
  38. $conn = mysql_connect($gDb['host'],$gDb['user'],$gDb['pwd']); 
  39. mysql_select_db($gDb['db']) or die('database connect error!');  
  40. mysql_query("SET NAMES 'gbk'");  
  41. ?> 

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表