我正在尝试将PHP提供的zip文件解压缩到我提供的路径以及每个文件名中,并且所有内容都100%正确,但是当我运行脚本时,它仍返回Err_Code 19。文件不是zip存档。主要模块是,当我运行相同的模块并从FTP或Dropbox或S3下载文件时,它会从Google驱动器下载zip文件,然后解压缩并还原整个站点,它工作得很好,但是从驱动器下载时,它开始提供我这个错误是我正在尝试的代码。
$extractTo = wp_upload_dir()['basedir'];
$zip = new ZipArchive;
$res = $zip->open($zipFile);
if (is_resource($res)) {
echo 'HERE'; exit;
$zip->extractTo($extractTo);
$zip->close();
$dirs = array_filter(glob($extractTo . '/*'), 'is_dir');
foreach ($dirs as $dir) {
$fileName = basename($dir);
if ($fileName == 'themes') {
$pathFrom = $extractTo . '/themes';
$pathTo = get_home_path() . 'wp-content\themes';
Helper::moveDirectories($pathFrom, $pathTo);
Helper::removeDirectories($pathFrom);
} else if ($fileName == 'files') {
$pathFrom = $extractTo . '/files';
$pathTo = get_home_path() . 'wp-content\uploads';
Helper::moveDirectories($pathFrom, $pathTo);
Helper::removeDirectories($pathFrom);
} else if ($fileName == 'plugins') {
$pathFrom = $extractTo . '/plugins';
$pathTo = get_home_path() . 'wp-content\plugins';
Helper::moveDirectories($pathFrom, $pathTo);
Helper::removeDirectories($pathFrom);
} else if ($fileName == 'database') {
$pathFrom = $extractTo . '/database';
$filename = array_filter(glob($pathFrom . '/*'));
Helper::DelDatabse();
Helper::importDatabase($filename[0]);
Helper::removeDirectories($pathFrom);
}
}
echo 'Done!';
} else {
echo $res;
}
不知道问题是什么或什么可能导致此奇怪的问题,我们将不胜感激。谢谢。
可能的重复
但是所有解决方案均无效。
答案 0 :(得分:1)
找到了解决方案,以防万一将来有人需要帮助,真正的问题不在提取代码中,而问题出在我下载该zip文件的代码中,真正的原因是我将下载的内容添加到了具有fwrite和fwrite的zip文件未正确压缩内容,因此,如果有人想从google驱动器下载zip文件,则他/她可以使用此代码将正常工作。
$client = new Google_Client();
$client->setClientId('CLIENT_ID');
$client->setClientSecret('CLIENT_SECRET');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setAccessToken($AccessToken);
$service = new Google_Service_Drive($client);
$fileId = $dir['drive_dir'];
$storeIn = wp_upload_dir()['basedir'] . '/' . $fileName . '.zip';
$content = $service->files->get($fileId, array('alt' => 'media'));
$headers = $content->getHeaders();
foreach ($headers as $name => $values) {
header($name . ': ' . implode(', ', $values));
}
header('Content-Disposition: attachment; filename="' . $fileName . '"');
$body = $content->getBody();
while (!$body->eof()) {
file_put_contents($storeIn, $body);
}
echo "Done.\n";