Exemple #1
0
/**
 * base64转图片
 * @param string $data base64的字符串
 * @return string 文件路径
 */
function base642img($data, $file = null)
{
    preg_match('/^(data:\\s*image\\/(\\w+);base64,)/', $data, $result);
    $file_ext = $result[2];
    $data = base64_decode(str_replace($result[1], '', $data));
    // file_put_contents($new_file, )
    $year = date('Y');
    $month = date('m');
    $day = date('d');
    $sub_file = $year . '-' . $month . '-' . $day;
    $root = '/Public/UploadFiles/' . $sub_file;
    $file = empty($file) ? dirname(dirname(__FILE__)) . $root : $file;
    if (!file_exists($file)) {
        recursiveMkdir($file);
    }
    $file_name = '/' . get_randomstr(12) . '.' . $file_ext;
    $res = file_put_contents($file . $file_name, $data);
    if ($res) {
        return $sub_file . $file_name;
    }
}
 /**
  * 备份数据库
  */
 public function backup()
 {
     //**判断是否有限权,显示登录管理员信息
     $id = $_SESSION['id'];
     //dump($id);
     //exit;
     $m = D('Admin');
     $arr = $m->find($id);
     $arr = $arr['admin_type'];
     //dump($arr);
     //exit;
     if ($arr == 1) {
         // 如果不是超级管理员限权
         $this->error('你不是超级管理员,没有限权!');
     }
     //exit;
     if (!IS_POST) {
         $this->error("Access Denied");
     }
     $M = M();
     //         dump($M);
     //         exit;
     //防止备份数据过程超时
     function_exists('set_time_limit') && set_time_limit(0);
     $tables = I('key', array(), '');
     //                 dump($tables);
     //                 exit;
     if (empty($tables)) {
         $this->error('请选择要备份的数据表');
     }
     $time = time();
     //开始时间
     $path = $this->getDbPath() . "/tuzicmstables_" . date("Ymd") . "_" . get_randomstr(5);
     //         dump($path);
     //         exit;
     $pre = "# -----------------------------------------------------------\n";
     //取得表结构信息
     //1,表示表名和字段名会用``包着的,0 则不用``
     //M()->query("SET SQL_QUOTE_SHOW_CREATE = 1"); //Log会有警告信息DbMysql.class.php(109|80)
     M()->execute("SET SQL_QUOTE_SHOW_CREATE = 1");
     $outstr = '';
     //         dump($path);
     //         exit;
     foreach ($tables as $table) {
         $outstr .= "# 表的结构 {$table} \n";
         $outstr .= "DROP TABLE IF EXISTS `{$table}`;\n";
         $tmp = $M->query("SHOW CREATE TABLE {$table}");
         $outstr .= $tmp[0]['create table'] . " ;\n\n";
     }
     $sqlTable = $outstr;
     $outstr = "";
     $file_n = 1;
     $backedTable = array();
     //         dump($sqlTable);
     //         exit;
     //表中的数据
     foreach ($tables as $table) {
         $backedTable[] = $table;
         $outstr .= "\n\n# 转存表中的数据:{$table} \n";
         $tableInfo = $M->query("SHOW TABLE STATUS LIKE '{$table}'");
         $page = ceil($tableInfo[0]['rows'] / 10000) - 1;
         for ($i = 0; $i <= $page; $i++) {
             $query = $M->query("SELECT * FROM {$table} LIMIT " . $i * 10000 . ", 10000");
             foreach ($query as $val) {
                 $temSql = "";
                 $tn = 0;
                 $temSql = '';
                 foreach ($val as $v) {
                     $temSql .= $tn == 0 ? "" : ",";
                     $temSql .= $v == '' ? "''" : "'{$v}'";
                     $tn++;
                 }
                 $temSql = "INSERT INTO `{$table}` VALUES ({$temSql});\n";
                 $sqlNo = "\n# Time: " . date("Y-m-d H:i:s") . "\n" . "# -----------------------------------------------------------\n" . "# SQLFile Label:#{$file_n}\n# -----------------------------------------------------------\n\n\n";
                 if ($file_n == 1) {
                     $sqlNo = "# Description:备份的数据表[结构]:" . implode(",", $tables) . "\n" . "# Description:备份的数据表[数据]:" . implode(",", $backedTable) . $sqlNo;
                 } else {
                     $sqlNo = "# Description:备份的数据表[数据]:" . implode(",", $backedTable) . $sqlNo;
                 }
                 if (strlen($pre) + strlen($sqlNo) + strlen($sqlTable) + strlen($outstr) + strlen($temSql) > C("CFG_SQL_FILESIZE")) {
                     $file = $path . "_" . $file_n . ".sql";
                     $outstr = $file_n == 1 ? $pre . $sqlNo . $sqlTable . $outstr : $pre . $sqlNo . $outstr;
                     if (!file_put_contents($file, $outstr, FILE_APPEND)) {
                         $this->error("备份文件写入失败!", U('Database/index'));
                     }
                     $sqlTable = $outstr = "";
                     $backedTable = array();
                     $backedTable[] = $table;
                     $file_n++;
                     dump($file_n);
                     exit;
                 }
                 $outstr .= $temSql;
             }
         }
     }
     if (strlen($sqlTable . $outstr) > 0) {
         $sqlNo = "\n# Time: " . date("Y-m-d H:i:s") . "\n" . "# -----------------------------------------------------------\n" . "# SQLFile Label:#{$file_n}\n# -----------------------------------------------------------\n\n\n";
         if ($file_n == 1) {
             $sqlNo = "# Description:备份的数据表[结构] " . implode(",", $tables) . "\n" . "# Description:备份的数据表[数据] " . implode(",", $backedTable) . $sqlNo;
         } else {
             $sqlNo = "# Description:备份的数据表[数据] " . implode(",", $backedTable) . $sqlNo;
         }
         $file = $path . "_" . $file_n . ".sql";
         //             dump($file);
         //             exit;
         $outstr = $file_n == 1 ? $pre . $sqlNo . $sqlTable . $outstr : $pre . $sqlNo . $outstr;
         //file_put_contents($file, $outstr, FILE_APPEND);
         if (!file_put_contents($file, $outstr, FILE_APPEND)) {
             $this->error("备份文件写入失败!", U('Database/index'));
         }
         $file_n++;
     }
     $time = time() - $time;
     $this->success("成功备份数据表,本次备份共生成了" . ($file_n - 1) . "个SQL文件。耗时:{$time} 秒", U('Database/restore'));
 }