index.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. include 'auto.php';
  12. if (IS_SAE)
  13. header("Location: index_sae.php");
  14. if (file_exists('./install.lock')) {
  15. echo '
  16. <html>
  17. <head>
  18. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  19. </head>
  20. <body>
  21. 你已经安装过该系统,如果想重新安装,请先删除站点install目录下的 install.lock 文件,然后再安装。
  22. </body>
  23. </html>';
  24. exit;
  25. }
  26. @set_time_limit(1000);
  27. if ('5.5.9' > phpversion()) {
  28. header("Content-type:text/html;charset=utf-8");
  29. exit('您的php版本过低,不能安装本软件,请升级到5.5.9或更高版本再安装,谢谢!');
  30. }
  31. define("CRMEB_VERSION", '20180601');
  32. date_default_timezone_set('PRC');
  33. error_reporting(E_ALL & ~E_NOTICE);
  34. header('Content-Type: text/html; charset=UTF-8');
  35. define('SITEDIR', _dir_path(substr(dirname(__FILE__), 0, -8)));//入口文件目录
  36. define('CRMEBDIR', _dir_path(substr(dirname(__FILE__), 0, -15)));//项目目录
  37. //数据库
  38. $sqlFile = 'zhishifufei.sql';
  39. $configFile = 'config.php';
  40. $databaseConfigFile = 'database_config.php';
  41. if (!file_exists(SITEDIR . 'install/' . $sqlFile) || !file_exists(SITEDIR . 'install/' . $configFile)|| !file_exists(SITEDIR . 'install/' . $databaseConfigFile)) {
  42. echo '缺少必要的安装文件!';
  43. exit;
  44. }
  45. $Title = "CrmEb知识付费安装向导";
  46. $Powered = "Powered by CrmEb";
  47. $steps = array(
  48. '1' => '安装许可协议',
  49. '2' => '运行环境检测',
  50. '3' => '安装参数设置',
  51. '4' => '安装详细过程',
  52. '5' => '安装完成',
  53. );
  54. $step = isset($_GET['step']) ? $_GET['step'] : 1;
  55. //地址
  56. $scriptName = !empty($_SERVER["REQUEST_URI"]) ? $scriptName = $_SERVER["REQUEST_URI"] : $scriptName = $_SERVER["PHP_SELF"];
  57. $rootpath = @preg_replace("/\/(I|i)nstall\/index\.php(.*)$/", "", $scriptName);
  58. $domain = empty($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
  59. if ((int)$_SERVER['SERVER_PORT'] != 80) {
  60. $domain .= ":" . $_SERVER['SERVER_PORT'];
  61. }
  62. $domain = $domain . $rootpath;
  63. switch ($step) {
  64. case '1':
  65. include_once("./templates/step1.php");
  66. exit();
  67. case '2':
  68. if (phpversion() < 5.6) {
  69. die('本系统需要PHP5+MYSQL >=5.5.9环境,当前PHP版本为:' . phpversion());
  70. }
  71. if(phpversion()==7.1){
  72. _copy(CRMEBDIR .'help/zsff/71/AuthBasic.php',CRMEBDIR .'extend/basic/AuthBasic.php');
  73. }
  74. if(phpversion()==7.2){
  75. _copy(CRMEBDIR .'help/zsff/72/AuthBasic.php',CRMEBDIR .'extend/basic/AuthBasic.php');
  76. }
  77. if(phpversion()==7.3){
  78. _copy(CRMEBDIR .'help/zsff/73/AuthBasic.php',CRMEBDIR .'extend/basic/AuthBasic.php');
  79. }
  80. $phpv = @ phpversion();
  81. $os = PHP_OS;
  82. //$os = php_uname();
  83. $tmp = function_exists('gd_info') ? gd_info() : array();
  84. $server = $_SERVER["SERVER_SOFTWARE"];
  85. $host = (empty($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_HOST"] : $_SERVER["SERVER_ADDR"]);
  86. $name = $_SERVER["SERVER_NAME"];
  87. $max_execution_time = ini_get('max_execution_time');
  88. $allow_reference = (ini_get('allow_call_time_pass_reference') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
  89. $allow_url_fopen = (ini_get('allow_url_fopen') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
  90. $safe_mode = (ini_get('safe_mode') ? '<font color=red>[×]On</font>' : '<font color=green>[√]Off</font>');
  91. $err = 0;
  92. if (empty($tmp['GD Version'])) {
  93. $gd = '<font color=red>[×]Off</font>';
  94. $err++;
  95. } else {
  96. $gd = '<font color=green>[√]On</font> ' . $tmp['GD Version'];
  97. }
  98. if (function_exists('mysqli_connect')) {
  99. $mysql = '<span class="correct_span">&radic;</span> 已安装';
  100. } else {
  101. $mysql = '<span class="correct_span error_span">&radic;</span> 请安装mysqli扩展';
  102. $err++;
  103. }
  104. if (extension_loaded('swoole_loader')) {
  105. $swoole = '<span class="correct_span">&radic;</span> 已安装';
  106. } else {
  107. $swoole = '<a href="http://help.crmeb.net/crmeb_zsff/1944228" target="_blank"><span class="correct_span error_span">&radic;</span> 点击查看帮助</a >';
  108. $err++;
  109. }
  110. if (extension_loaded('redis')) {
  111. $redisk = '<span class="correct_span">&radic;</span> 已安装';
  112. } else {
  113. $redisk = '<a href="http://help.crmeb.net/crmeb_zsff/1863590" target="_blank"><span class="correct_span error_span">&radic;</span> 点击查看帮助</a >';
  114. $err++;
  115. }
  116. if (extension_loaded('fileinfo')) {
  117. $fileinfo = '<span class="correct_span">&radic;</span> 已安装';
  118. } else {
  119. $fileinfo = '<a href="http://help.crmeb.net/crmeb_zsff/1863590" target="_blank"><span class="correct_span error_span">&radic;</span> 点击查看帮助</a >';
  120. $err++;
  121. }
  122. if (ini_get('file_uploads')) {
  123. $uploadSize = '<span class="correct_span">&radic;</span> ' . ini_get('upload_max_filesize');
  124. } else {
  125. $uploadSize = '<span class="correct_span error_span">&radic;</span>禁止上传';
  126. }
  127. if (function_exists('session_start')) {
  128. $session = '<span class="correct_span">&radic;</span> 支持';
  129. } else {
  130. $session = '<span class="correct_span error_span">&radic;</span> 不支持';
  131. $err++;
  132. }
  133. if (function_exists('curl_init')) {
  134. $curl = '<font color=green>[√]支持</font> ';
  135. } else {
  136. $curl = '<font color=red>[×]不支持</font>';
  137. $err++;
  138. }
  139. if (function_exists('file_put_contents')) {
  140. $file_put_contents = '<font color=green>[√]支持</font> ';
  141. } else {
  142. $file_put_contents = '<font color=red>[×]不支持</font>';
  143. $err++;
  144. }
  145. if (function_exists('bcadd')) {
  146. $BC = '<font color=green>[√]支持</font> ';
  147. } else {
  148. $BC = '<font color=red>[×]不支持</font>';
  149. $err++;
  150. }
  151. if (function_exists('openssl_encrypt')) {
  152. $openssl = '<font color=green>[√]支持</font> ';
  153. } else {
  154. $openssl = '<font color=red>[×]不支持</font>';
  155. $err++;
  156. }
  157. $folder = array(
  158. 'public/install',
  159. 'public/uploads',
  160. 'runtime',
  161. 'runtime/cache',
  162. 'runtime/temp',
  163. 'runtime/log',
  164. 'runtime/schema',
  165. 'application/database.php',
  166. 'application/config.php',
  167. );
  168. include_once("./templates/step2.php");
  169. exit();
  170. case '3':
  171. $dbName = strtolower(trim($_POST['dbName']));
  172. $_POST['dbport'] = $_POST['dbport'] ? $_POST['dbport'] : '3306';
  173. if ($_GET['testdbpwd']) {
  174. $dbHost = $_POST['dbHost'];
  175. $conn = @mysqli_connect($dbHost, $_POST['dbUser'], $_POST['dbPwd'], NULL, $_POST['dbport']);
  176. if (mysqli_connect_errno($conn)) {
  177. die(json_encode(0));
  178. } else {
  179. $result = mysqli_query($conn, "SELECT @@global.sql_mode");
  180. $result = $result->fetch_array();
  181. $version = mysqli_get_server_info($conn);
  182. if ($version >= 5.7) {
  183. if (strstr($result[0], 'STRICT_TRANS_TABLES') || strstr($result[0], 'STRICT_ALL_TABLES') || strstr($result[0], 'TRADITIONAL') || strstr($result[0], 'ANSI'))
  184. exit(json_encode(-1));
  185. }
  186. $result = mysqli_query($conn, "select count(table_name) as c from information_schema.`TABLES` where table_schema='$dbName'");
  187. $result = $result->fetch_array();
  188. if ($result['c'] > 0)
  189. exit(json_encode(-2));
  190. exit(json_encode(1));
  191. }
  192. }
  193. include_once("./templates/step3.php");
  194. exit();
  195. case '4':
  196. if (intval($_GET['install'])) {
  197. $n = intval($_GET['n']);
  198. if ($i == 999999)
  199. exit;
  200. $arr = array();
  201. $dbHost = trim($_POST['dbhost']);
  202. $_POST['dbport'] = $_POST['dbport'] ? $_POST['dbport'] : '3306';
  203. $dbName = strtolower(trim($_POST['dbname']));
  204. $dbUser = trim($_POST['dbuser']);
  205. $dbPwd = trim($_POST['dbpw']);
  206. $dbPrefix = empty($_POST['dbprefix']) ? 'eb_' : trim($_POST['dbprefix']);
  207. $username = trim($_POST['manager']);
  208. $password = trim($_POST['manager_pwd']);
  209. $email = trim($_POST['manager_email']);
  210. if (!function_exists('mysqli_connect')) {
  211. $arr['msg'] = "请安装 mysqli 扩展!";
  212. echo json_encode($arr);
  213. exit;
  214. }
  215. $conn = @mysqli_connect($dbHost, $dbUser, $dbPwd, NULL, $_POST['dbport']);
  216. if (mysqli_connect_errno($conn)) {
  217. $arr['msg'] = "连接数据库失败!" . mysqli_connect_error($conn);
  218. echo json_encode($arr);
  219. exit;
  220. }
  221. mysqli_set_charset($conn, "utf8");
  222. $version = mysqli_get_server_info($conn);
  223. if ($version < 5.1) {
  224. $arr['msg'] = '数据库版本太低! 必须5.1以上';
  225. echo json_encode($arr);
  226. exit;
  227. }
  228. if (!mysqli_select_db($conn, $dbName)) {
  229. //创建数据时同时设置编码
  230. if (!mysqli_query($conn, "CREATE DATABASE IF NOT EXISTS `" . $dbName . "` DEFAULT CHARACTER SET utf8;")) {
  231. $arr['msg'] = '数据库 ' . $dbName . ' 不存在,也没权限创建新的数据库!';
  232. echo json_encode($arr);
  233. exit;
  234. }
  235. if ($n == -1) {
  236. $arr['n'] = 0;
  237. $arr['msg'] = "成功创建数据库:{$dbName}<br>";
  238. echo json_encode($arr);
  239. exit;
  240. }
  241. mysqli_select_db($conn, $dbName);
  242. }
  243. //读取数据文件
  244. $sqldata = file_get_contents(SITEDIR . 'install/' . $sqlFile);
  245. $sqlFormat = sql_split($sqldata, $dbPrefix);
  246. //创建写入sql数据库文件到库中 结束
  247. /**
  248. * 执行SQL语句
  249. */
  250. $counts = count($sqlFormat);
  251. for ($i = $n; $i < $counts; $i++) {
  252. $sql = trim($sqlFormat[$i]);
  253. if (strstr($sql, 'CREATE TABLE')) {
  254. preg_match('/CREATE TABLE `eb_([^ ]*)`/is', $sql, $matches);
  255. mysqli_query($conn, "DROP TABLE IF EXISTS `$matches[1]");
  256. $sql = str_replace('`eb_', '`' . $dbPrefix, $sql);//替换表前缀
  257. $ret = mysqli_query($conn, $sql);
  258. if ($ret) {
  259. $message = '<li><span class="correct_span">&radic;</span>创建数据表[' . $dbPrefix . $matches[1] . ']完成!<span style="float: right;">' . date('Y-m-d H:i:s') . '</span></li> ';
  260. } else {
  261. $message = '<li><span class="correct_span error_span">&radic;</span>创建数据表[' . $dbPrefix . $matches[1] . ']失败!<span style="float: right;">' . date('Y-m-d H:i:s') . '</span></li>';
  262. }
  263. $i++;
  264. $arr = array('n' => $i, 'msg' => $message);
  265. echo json_encode($arr);
  266. exit;
  267. } else {
  268. if (trim($sql) == '')
  269. continue;
  270. $sql = str_replace('`eb_', '`' . $dbPrefix, $sql);//替换表前缀
  271. $ret = mysqli_query($conn, $sql);
  272. $message = '';
  273. $arr = array('n' => $i, 'msg' => $message);
  274. }
  275. }
  276. // 清空测试数据
  277. if (!$_POST['demo']) {
  278. $result = mysqli_query($conn, "show tables");
  279. $tables = mysqli_fetch_all($result);//参数MYSQL_ASSOC、MYSQLI_NUM、MYSQLI_BOTH规定产生数组类型
  280. $bl_table = array('eb_system_admin'
  281. , 'eb_system_role'
  282. , 'eb_system_config'
  283. , 'eb_system_config_tab'
  284. , 'eb_system_config_content'
  285. , 'eb_system_menus'
  286. , 'eb_system_file'
  287. , 'eb_express'
  288. , 'eb_system_group'
  289. , 'eb_system_group_data'
  290. , 'eb_member_ship'
  291. , 'eb_live_gift'
  292. , 'eb_recommend'
  293. , 'eb_wechat_template'
  294. , 'eb_routine_template');
  295. foreach ($bl_table as $k => $v) {
  296. $bl_table[$k] = str_replace('eb_', $dbPrefix, $v);
  297. }
  298. foreach ($tables as $key => $val) {
  299. if (!in_array($val[0], $bl_table)) {
  300. mysqli_query($conn, "truncate table " . $val[0]);
  301. }
  302. }
  303. delFile(CRMEBDIR . '/public/uploads'); // 清空测试图片
  304. }
  305. //读取配置文件,并替换真实配置数据1
  306. $strConfig = file_get_contents(SITEDIR . 'install/' . $databaseConfigFile);
  307. $strConfig = str_replace('#DB_HOST#', $dbHost, $strConfig);
  308. $strConfig = str_replace('#DB_NAME#', $dbName, $strConfig);
  309. $strConfig = str_replace('#DB_USER#', $dbUser, $strConfig);
  310. $strConfig = str_replace('#DB_PWD#', $dbPwd, $strConfig);
  311. $strConfig = str_replace('#DB_PORT#', $_POST['dbport'], $strConfig);
  312. $strConfig = str_replace('#DB_PREFIX#', $dbPrefix, $strConfig);
  313. $strConfig = str_replace('#DB_CHARSET#', 'utf8', $strConfig);
  314. @chmod(CRMEBDIR . '/application/database.php', 0777); //数据库配置文件的地址
  315. @file_put_contents(CRMEBDIR . '/application/database.php', $strConfig); //数据库配置文件的地址
  316. //redis数据库信息
  317. $rbhost = $_POST['rbhost'] ? $_POST['rbhost'] : '127.0.0.1';
  318. $rbport = $_POST['rbport'] ? $_POST['rbport'] : '6379';
  319. $rbpw = $_POST['rbpw'] ? $_POST['rbpw'] : '';
  320. $strConfig = file_get_contents(SITEDIR . 'install/' . $configFile);
  321. $strConfig = str_replace('#RB_HOST#', $rbhost, $strConfig);
  322. $strConfig = str_replace('#RB_PORT#', $rbport, $strConfig);
  323. $strConfig = str_replace('#RB_PWD#', $rbpw, $strConfig);
  324. @chmod(CRMEBDIR . '/application/config.php', 0777); //redis配置文件的地址
  325. @file_put_contents(CRMEBDIR . '/application/config.php', $strConfig); //redis配置文件的地址
  326. //更新网站配置信息2
  327. //插入管理员表字段tp_admin表
  328. $time = time();
  329. $ip = get_client_ip();
  330. $ip = empty($ip) ? "0.0.0.0" : $ip;
  331. $password = md5(trim($_POST['manager_pwd']));
  332. mysqli_query($conn, "truncate table {$dbPrefix}system_admin");
  333. $addadminsql = "INSERT INTO `{$dbPrefix}system_admin` (`id`, `account`, `pwd`, `real_name`, `roles`, `last_ip`, `last_time`, `add_time`, `login_count`, `level`, `status`, `is_del`) VALUES
  334. (1, '" . $username . "', '" . $password . "', 'admin', '1', '" . $ip . "',$time , $time, 0, 0, 1, 0)";
  335. $res = mysqli_query($conn, $addadminsql);
  336. if(isset($_SERVER['SERVER_NAME'])) {
  337. $site_url = '\'"http://' . $_SERVER['SERVER_NAME'].'"\'';
  338. $res2 = mysqli_query($conn, 'UPDATE `'.$dbPrefix.'system_config` SET `value`=' . $site_url . ' WHERE `menu_name`="site_url"');
  339. }
  340. if ($res) {
  341. $message = '成功添加管理员<br />成功写入配置文件<br>安装完成.';
  342. $arr = array('n' => 999999, 'msg' => $message);
  343. echo json_encode($arr);
  344. exit;
  345. } else {
  346. $message = '添加管理员失败<br />成功写入配置文件<br>安装完成.';
  347. $arr = array('n' => 999999, 'msg' => $message);
  348. echo json_encode($arr);
  349. exit;
  350. }
  351. }
  352. include_once("./templates/step4.php");
  353. exit();
  354. case '5':
  355. $ip = get_client_ip();
  356. $host = $_SERVER['HTTP_HOST'];
  357. $curent_version = getversion();
  358. $time = time();
  359. $mt_rand_str = $create_date . sp_random_string(6);
  360. $str_constant = "<?php" . PHP_EOL . "define('INSTALL_DATE'," . $time . ");" . PHP_EOL . "define('SERIALNUMBER','" . $mt_rand_str . "');";
  361. @file_put_contents(SITEDIR . '/application/constant.php', $str_constant);
  362. include_once("./templates/step5.php");
  363. @touch('./install.lock');
  364. exit();
  365. }
  366. //读取版本号
  367. function getversion(){
  368. $version_arr = [];
  369. $curent_version = @file(dirname(dirname(__DIR__)).'/.version');
  370. foreach ($curent_version as $val){
  371. list($k,$v)=explode('=',$val);
  372. $version_arr[$k]=$v;
  373. }
  374. return $version_arr;
  375. }
  376. function testwrite($d)
  377. {
  378. if (is_file($d)) {
  379. if (is_writeable($d)) {
  380. return true;
  381. }
  382. return false;
  383. } else {
  384. $tfile = "_test.txt";
  385. $fp = @fopen($d . "/" . $tfile, "w");
  386. if (!$fp) {
  387. return false;
  388. }
  389. fclose($fp);
  390. $rs = @unlink($d . "/" . $tfile);
  391. if ($rs) {
  392. return true;
  393. }
  394. return false;
  395. }
  396. }
  397. function sql_split($sql, $tablepre)
  398. {
  399. if ($tablepre != "tp_")
  400. $sql = str_replace("tp_", $tablepre, $sql);
  401. $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql);
  402. $sql = str_replace("\r", "\n", $sql);
  403. $ret = array();
  404. $num = 0;
  405. $queriesarray = explode(";\n", trim($sql));
  406. unset($sql);
  407. foreach ($queriesarray as $query) {
  408. $ret[$num] = '';
  409. $queries = explode("\n", trim($query));
  410. $queries = array_filter($queries);
  411. foreach ($queries as $query) {
  412. $str1 = substr($query, 0, 1);
  413. if ($str1 != '#' && $str1 != '-')
  414. $ret[$num] .= $query;
  415. }
  416. $num++;
  417. }
  418. return $ret;
  419. }
  420. function _dir_path($path)
  421. {
  422. $path = str_replace('\\', '/', $path);
  423. if (substr($path, -1) != '/')
  424. $path = $path . '/';
  425. return $path;
  426. }
  427. // 获取客户端IP地址
  428. function get_client_ip()
  429. {
  430. static $ip = NULL;
  431. if ($ip !== NULL)
  432. return $ip;
  433. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  434. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  435. $pos = array_search('unknown', $arr);
  436. if (false !== $pos)
  437. unset($arr[$pos]);
  438. $ip = trim($arr[0]);
  439. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  440. $ip = $_SERVER['HTTP_CLIENT_IP'];
  441. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  442. $ip = $_SERVER['REMOTE_ADDR'];
  443. }
  444. // IP地址合法验证
  445. $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
  446. return $ip;
  447. }
  448. function dir_create($path, $mode = 0777)
  449. {
  450. if (is_dir($path))
  451. return TRUE;
  452. $ftp_enable = 0;
  453. $path = dir_path($path);
  454. $temp = explode('/', $path);
  455. $cur_dir = '';
  456. $max = count($temp) - 1;
  457. for ($i = 0; $i < $max; $i++) {
  458. $cur_dir .= $temp[$i] . '/';
  459. if (@is_dir($cur_dir))
  460. continue;
  461. @mkdir($cur_dir, 0777, true);
  462. @chmod($cur_dir, 0777);
  463. }
  464. return is_dir($path);
  465. }
  466. function dir_path($path)
  467. {
  468. $path = str_replace('\\', '/', $path);
  469. if (substr($path, -1) != '/')
  470. $path = $path . '/';
  471. return $path;
  472. }
  473. function sp_password($pw, $pre)
  474. {
  475. $decor = md5($pre);
  476. $mi = md5($pw);
  477. return substr($decor, 0, 12) . $mi . substr($decor, -4, 4);
  478. }
  479. function sp_random_string($len = 8)
  480. {
  481. $chars = array(
  482. "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
  483. "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
  484. "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
  485. "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  486. "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
  487. "3", "4", "5", "6", "7", "8", "9"
  488. );
  489. $charsLen = count($chars) - 1;
  490. shuffle($chars); // 将数组打乱
  491. $output = "";
  492. for ($i = 0; $i < $len; $i++) {
  493. $output .= $chars[mt_rand(0, $charsLen)];
  494. }
  495. return $output;
  496. }
  497. // 递归删除文件夹
  498. function delFile($dir, $file_type = '')
  499. {
  500. if (is_dir($dir)) {
  501. $files = scandir($dir);
  502. //打开目录 //列出目录中的所有文件并去掉 . 和 ..
  503. foreach ($files as $filename) {
  504. if ($filename != '.' && $filename != '..') {
  505. if (!is_dir($dir . '/' . $filename)) {
  506. if (empty($file_type)) {
  507. unlink($dir . '/' . $filename);
  508. } else {
  509. if (is_array($file_type)) {
  510. //正则匹配指定文件
  511. if (preg_match($file_type[0], $filename)) {
  512. unlink($dir . '/' . $filename);
  513. }
  514. } else {
  515. //指定包含某些字符串的文件
  516. if (false != stristr($filename, $file_type)) {
  517. unlink($dir . '/' . $filename);
  518. }
  519. }
  520. }
  521. } else {
  522. delFile($dir . '/' . $filename);
  523. rmdir($dir . '/' . $filename);
  524. }
  525. }
  526. }
  527. } else {
  528. if (file_exists($dir)) unlink($dir);
  529. }
  530. }
  531. function _copy($file, $newFile)
  532. {
  533. if (file_exists($file)) {
  534. copy($file, $newFile); //拷贝到新目录
  535. }
  536. }
  537. ?>