首页 > 解决方案 > PHP | 我如何让我的 php 脚本更新程序检查远程文本文件中丢失的版本?

问题描述

我正在为我的 CMS 项目开发 PHP 更新程序。它的作用是检查远程version.txt文件以查看它是否需要更新。我的问题是,什么是用户仍在版本1.0上,而远程version.txt文件中定义的最新版本是1.3?如何让我的更新程序按顺序更新到1.2版和1.3版?我在下面提供了我的代码。

<?php

$phoenix_version = 1.0;
         $rc = @fsockopen("www.phoenix.ltda", 80, $errno, $errstr, 1);
        if (is_resource($rc))  {
        /* Update Version */
        $remote_version=trim(file_get_contents('https://phoenix.ltda/updates/version.txt'));
        $remote_version = preg_replace('/[^\\d.]+/', '', $remote_version);
        /* Current up to date version */
        $remote_current_version=trim(file_get_contents('https://phoenix.ltda/updates/current-version.txt'));
        $remote_current_version = preg_replace('/[^\\d.]+/', '', $remote_current_version);
        if(version_compare($remote_version, $phoenix_version) ==  1){
            $new_version = true;
        }else{
            $new_version = false; 
        }
        
        }else{
        echo "The Phoenix PHP updater could not be reached by this server. Please try again later or contact the Phoenix PHP team.";
        }
        
        
        if(!empty($_POST)) {
            $_POST['update'] = Database::clean_string($_POST['update']);
            $values['update'] = $_POST['update'];
            
            $clean_remote_version = preg_replace('/[.,]/', '', $remote_version);
            
            $ch = curl_init();
            $source = 'https://phoenix.ltda/updates/phoenix-update'.$clean_remote_version.'.zip';
            curl_setopt($ch, CURLOPT_URL, $source);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec ($ch);
            curl_close ($ch);

            $destination = ROOT_PATH . '/phoenix-update'.$clean_remote_version.'.zip';
            $file = fopen($destination, "w+");
            fputs($file, $data);
            fclose($file);

        }
?>

如何让它更新到最新版本?像 1.0 到 1.1 到 1.2 一直到最新版本?解决此问题的最佳方法是什么?

标签: php

解决方案


如果更新只是代码更新,这不是问题。但是当数据库有多个更新时,您需要编写数据库迁移并逐个版本地运行它们(查看 Laravel 或 Sphinx 以获得很好的示例)。此外,您的更新脚本非常复杂;最好将当前版本发布到 API 上并让它以最终更新的版本响应 - 这也可以用于发送更新批处理 URL,而不是获取单个文件。好吧,如果 API 只响应下一个版本(而不是最新版本),这甚至可以工作 - 但批量运行可能会更优雅。


推荐阅读