I have published a similar article beforePHP obtains the remote .sql.zip file, then clears the data, and then imports the .sql.zip into the mysql database》, but here is another function, which is also available for personal testing!
隐藏内容:会员可查看
/**
* 从远程URL下载包含SQL文件的ZIP压缩文件,解压缩SQL文件到指定目录,然后将数据导入到MySQL数据库中。
*
* @param string $remoteUrl 远程ZIP文件的URL地址
* @param string $dbName 目标MySQL数据库的名称
* @param string $dbUser 目标MySQL数据库的用户名
* @param string $dbPass 目标MySQL数据库的密码
* @param string $dbHost 目标MySQL数据库的主机地址,默认为localhost
* @param string $sqlDir 解压缩SQL文件的目录路径,默认为当前文件所在目录下的path/目录
* @throws Exception 如果下载、解压缩、数据库操作失败,则会抛出异常
*/
function importSqlZip($remoteUrl, $dbName, $dbUser, $dbPass, $dbHost = 'localhost', $sqlDir = __DIR__.'/path/') {
// 创建一个临时文件用于保存下载的 Zip 文件
$tempZip = tempnam(sys_get_temp_dir(), 'sql_zip_');
// 下载远程 Zip 文件并保存到本地
$fp = fopen($tempZip, 'w');
$ch = curl_init($remoteUrl);
curl_setopt($ch, CURLOPT_FILE, $fp);
$success = curl_exec($ch);
curl_close($ch);
fclose($fp);
if (!$success) {
throw new Exception('Failed to download the remote SQL ZIP file.');
}
// 创建指定目录
if (!mkdir($sqlDir, 0777, true)) {
throw new Exception('Failed to create directory.');
}
// 解压缩文件到指定目录
$zip = new ZipArchive();
if ($zip->open($tempZip) === TRUE) {
$zip->extractTo($sqlDir);
$zip->close();
} else {
throw new Exception('Failed to extract the SQL ZIP file.');
}
// 清空数据库
$dsn = "mysql:host=$dbHost;dbname=$dbName";
$pdo = new PDO($dsn, $dbUser, $dbPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
foreach ($tables as $table) {
$pdo->query("DROP TABLE IF EXISTS $table");
}
// 导入 SQL 文件
$files = glob($sqlDir . '*.sql');
foreach ($files as $file) {
$pdo->exec(file_get_contents($file));
}
// 删除临时文件和 SQL 文件
unlink($tempZip);
$files = glob($sqlDir . '*.sql');
foreach ($files as $file) {
unlink($file);
}
rmdir($sqlDir);
}
When you want to call this function, you need to call it in the following format:
importSqlZip($remoteUrl, $dbName, $dbUser, $dbPass, $dbHost, $sqlDir)
Among them, $remoteUrl is the URL of the remote SQL ZIP file, $dbName is the name of the MySQL database to be imported, $dbUser is the user name of the MySQL database, $dbPass is the password of the MySQL database, $dbHost is the host address of the MySQL database, the default is localhost, $sqlDir is the storage path of the decompressed SQL file, the default is the "path/" folder in the directory of the current PHP file.
For example, if you want to import data from "https://example.com/backup.zip" into the "my_database" database, using the username "my_user" and password "my_password", the default database host address and the SQL file storage path, you should call the function like this:
importSqlZip('https://example.com/backup.zip', 'my_database', 'my_user', 'my_password');
If you want to customize the database host address and SQL file storage path, you can call the function like this:
importSqlZip('https://example.com/backup.zip', 'my_database', 'my_user', 'my_password', 'my_db_host', '/custom/sql/dir/');