basePath . '/runtime/' . $this->file; if (!file_exists($file)) { return; } // 判断runtime目录下是否存在import目录,如果有就删除 $import_dir = \Yii::$app->basePath . '/runtime/import/'; if (is_dir($import_dir)) { $this->deleteDirectory($import_dir); } // 解压缩文件 $zip = new \ZipArchive(); if ($zip->open($file) === true) { $zip->extractTo(\Yii::$app->basePath . '/runtime/import/'); $zip->close(); } else { return; } $this->importSql(); $this->importFile(); // 删除解压缩后的文件 $this->deleteDirectory(\Yii::$app->basePath . '/runtime/import/'); // 删除锁文件 $lock_file = \Yii::$app->basePath . '/runtime/import.lock'; if (file_exists($lock_file)) { unlink($lock_file); } } // 执行sql public function importSql() { try { // 删除option表中store_id为1的记录 Option::deleteAll(['store_id' => 1]); // 删除about_article表中store_id为1的记录 AboutArticle::deleteAll(['store_id' => 1]); Article::deleteAll(['store_id' => 1]); AccountLog::deleteAll(['store_id' => 1]); NewDiyTemplate::deleteAll(['store_id' => 1]); Address::deleteAll(['store_id' => 1]); // 删除admin表中id 不为1 的记录 Admin::deleteAll(['!=', 'id', 1]); AdminRole::deleteAll(); // 获取runtime目录下的sql文件 // 设置max_allowed_packet \Yii::$app->db->createCommand('SET GLOBAL max_allowed_packet=1073741824')->execute(); $sql_files = glob(\Yii::$app->basePath . '/runtime/import/sql/*.sql'); foreach ($sql_files as $file) { $sql = file_get_contents($file); // 执行sql try { \Yii::$app->db->createCommand($sql)->execute(); } catch (\Exception $ex) { // 记录具体SQL文件执行错误 debug_log($ex->getMessage(), 'import_sql.log'); continue; } } } catch (\Throwable $e) { // 记录错误日志 debug_log($e->getMessage(), 'import_sql.log'); return; } } public function importFile() { try { // 判断web/uploads目录下是否存在images/store_1目录, 没有就添加 $image_path = \Yii::$app->basePath . '/web/uploads/images/store_1/'; if (!is_dir($image_path)) { mkdir($image_path, 0777, true); } $this->copyDirectory(\Yii::$app->basePath . '/runtime/import/uploads/images/', $image_path); // 判断web/uploads目录下是否存在videos/store_1目录, 没有就添加 $video_path = \Yii::$app->basePath . '/web/uploads/videos/store_1/'; if (!is_dir($video_path)) { mkdir($video_path, 0777, true); } $this->copyDirectory(\Yii::$app->basePath . '/runtime/import/uploads/videos/', $video_path); } catch (\Throwable $e) { // 记录错误日志 debug_log($e->getMessage(), 'import_file.log'); return; } } // 删除文件夹 public function deleteDirectory($dir) { if (!is_dir($dir)) { return; } $files = scandir($dir); foreach ($files as $file) { if ($file != '.' && $file != '..') { if (is_dir($dir . '/' . $file)) { $this->deleteDirectory($dir . '/' . $file); } else { unlink($dir . '/' . $file); } } } rmdir($dir); } public function copyDirectory($source, $destination) { if (!is_dir($source)) { return; } if (!is_dir($destination)) { mkdir($destination, 0777, true); } $files = scandir($source); foreach ($files as $file) { if ($file != '.' && $file != '..') { if (is_dir($source . '/' . $file)) { $this->copyDirectory($source . '/' . $file, $destination . '/' . $file); } else { copy($source . '/' . $file, $destination . '/' . $file); } } } } }