【问题标题】:PHP | How would I download and extract a ZIP file from a remote server?PHP |如何从远程服务器下载和提取 ZIP 文件?
【发布时间】:2020-09-09 21:50:21
【问题描述】:

我正在制作一个自动更新系统,但在弄清楚如何将 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();

?>

如何使用 PHP 从我的服务器获取更新 ZIP 并将其提取到根目录(public_html 或 httpdocs)?其他问题只提到了读取 ZIP 文件。

【问题讨论】:

  • 您可能想检查其他问题,例如stackoverflow.com/questions/3938534/…
  • cURL 或 file_get_contents:取决于您需要随请求发送的内容,file_get_contents 仅支持基本请求。您需要将纯粹的 $remote_version 保存到具有 file_put_contents 或类似文件的文件中,然后使用 php.net/manual/en/ziparchive.extractto.php 解压缩。摆脱那个 trim/preg_replace 垃圾。摆脱那个无用的定义。

标签: php file-get-contents


【解决方案1】:

这是 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/");

【讨论】:

    猜你喜欢
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多