-1

I am making an auto-update system, but I am having some trouble figuring out how to download and extract a ZIP file onto my server. I dont know whether to use cURL or file_get_contents. So far, I have the code that checks for the right version and displays a dialog. My code is below.

<?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();

?>

How would I fetch the update ZIP from my server and extract it to the root directory (public_html or httpdocs) with PHP? Other questions only mentioned reading the ZIP file.

  • You might want to check other questions like https://stackoverflow.com/questions/3938534/download-file-to-server-from-url – Progman Sep 09 '20 at 22:36
  • cURL or file_get_contents: depends on what you need to send with the request, file_get_contents only supports basic requests. You need to save the unadulterated $remote_version to a file with file_put_contents or similar and then unzip it with https://www.php.net/manual/en/ziparchive.extractto.php. Get rid of that trim/preg_replace junk. Get rid of that useless define while your at it. – Alex Barker Sep 10 '20 at 03:02

1 Answers1

0

Here's a ZipArchive class in php, you can extract zip directory with the help of this and refer this link for more help 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/");
Sapna Dorbi
  • 115
  • 2
  • 12