0) { $dataStr = 'INSERT INTO `'.$tableName.'` VALUE '; foreach ($data as $k => $item) { $valStr = '('; foreach ($item as $val) { // 字符串转义 $val = addslashes($val); $valStr .= '"'.$val.'", '; } // 去除最后的逗号和空格 $valStr = substr($valStr,0,strlen($valStr)-2); // 限制单条sql语句在16K以内(可根据mysql配置条件进行调整) $sqlLength = strlen($dataStr) + strlen($valStr); if($sqlLength >= (1024 * 16 - 10)) { $dataStr .= $valStr.');'.$eol; file_put_contents($fileName, $dataStr, FILE_APPEND); // 提前分段写入后需重置数据语句 if ($k <= ($count-1)) { $dataStr = 'INSERT INTO `'.$tableName.'` VALUE '; } else { $dataStr = ''; } } else { if ($k < ($count-1)) { $dataStr .= $valStr.'),'.$eol; } else { $dataStr .= $valStr.');'.$eolB; } } } file_put_contents($fileName, $dataStr, FILE_APPEND); } } clearstatcache(); $backups = explode('/', $fileName); $backupName = $backups[count($backups)-1]; $src = Config::get('filesystem.disks.backup.url') . '/'; return $this->json(0, '备份成功:' . $src . $backupName); } public function index() { $path = Config::get('filesystem.disks.backup.root') . '/'; $src = Config::get('filesystem.disks.backup.url') . '/'; $items = []; if(is_dir($path)) { if(!is_readable($path)) { chmod($path,0755); } $files = scandir($path); foreach ($files as $file) { if($file != '.' && $file != '..') { $ext = substr($file, -4); if ($ext == '.sql') { $creatTime = substr($file, -18, 14); $items[] = [ 'file' => $file, 'path' => $src . $file, 'time' =>is_numeric($creatTime) ? date('Y-m-d H:i:s', strtotime($creatTime)) : '', ]; } } } } clearstatcache(); $this->data['items'] = $items; $this->data['backupPath'] = str_replace('\\','/', $path); return $this->view(); } public function del() { if (request()->isPost()) { $filePath = input('post.id'); Tool::delFile($filePath); MLog::write('backup', 'del', '删除了备份数据文件,文件路径为:' . $filePath); return $this->json(); } else { return $this->json(1, '非法请求'); } } }