此代码的目的是从远程服务器获取 update.zip 文件,将其解压缩并将其保存到本地目录,更新、覆盖或创建更新的文件。
我几乎已经有了这个工作的非 cURL 版本,但我宁愿使用这个版本。我遇到的第一个问题是 tmp 文件夹的路径不正确。我需要一种更好的嗅探方法(暂时硬编码)......
第二个问题是代码不起作用,但它没有引发错误。它执行 $x 分支,但没有进行 zip 提取。
require('../../../wp-blog-header.php'); //enables wp security check and ABSPATH
$payload = file_get_contents('http://myserver.com/upgrade.zip'); //grab the file from the remote server
$target = ABSPATH .'wp-content/themes/mytheme/'; // this is the destination for the unzipped files
openZip($payload);
function openZip($file_to_open, $debug = false) {
global $target;
$file = ABSPATH . '/tmp/'.md5($file_to_open).'.zip'; //this should be home/myfolder/tmp but ABSPATH is giving the wrong path to the tmp directory.
$client = curl_init($file_to_open);
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);
$fileData = curl_exec($client);
file_put_contents($file, $fileData);
$zip = new ZipArchive();
$x = $zip->open($file);
if($x === true) { //this is true, but no zip extraction?
$zip->extractTo($target);
$zip->close();
unlink($file);
} else {
if($debug !== true) {
unlink($file);
}
die("There was a problem. Please try again!");
}
}