php - PHP | 如何从远程服务器下载和提取 ZIP 文件?
问题描述
我正在制作一个自动更新系统,但在弄清楚如何将 ZIP 文件下载并解压缩到我的服务器上时遇到了一些麻烦。我不知道是使用 cURL 还是 file_get_contents。到目前为止,我有检查正确版本并显示对话框的代码。我的代码如下。
<?php
function check_for_updates(){
$phoenix_version = '1.0.0';
$rc = @fsockopen("www.phoenix.ltda", 80, $errno, $errstr, 1);
if (is_resource($rc)) {
define('REMOTE_VERSION', 'https://phoenix.ltda/updates/version.txt');
$remote_version=trim(file_get_contents(REMOTE_VERSION));
$remote_version = preg_replace('/[^\\d.]+/', '', $remote_version);
if(version_compare($remote_version, $phoenix_version) == 1){
echo 'New Version Detected!';
}
}
}
}
check_for_updates();
?>
如何从我的服务器获取更新 ZIP 并使用 PHP 将其提取到根目录(public_html 或 httpdocs)?其他问题只提到了阅读 ZIP 文件。
解决方案
这是 php 中的 ZipArchive 类,您可以借助它提取 zip 目录并参考此链接以获得更多帮助PHP:ZipArchive-Manual
public function unzip($source, $destination) {
@mkdir($destination, 0777, true);
foreach ((array) glob($source . "/*.zip") as $key => $value) {
$zip = new ZipArchive;
if ($zip->open(str_replace("//", "/", $value)) === true) {
$zip->extractTo($destination);
$zip->close();
}
}
}
//$zip->unzip("images_zip/", "images/");