服务器到服务器 > 检索远程 zip 文件并将其解压到本地服务器目录

2023-12-23

我在服务器 B 上有一个 wp 插件文件,其目的是从远程服务器 A 检索 zip 文件。

服务器 B 收到 zip 文件后,应提取内容并将文件复制到服务器 B 上的特定文件夹中,覆盖任何现有文件。

我从下面的文件中借用了一些代码,该文件使用上传器来执行相同的操作,我只想为上述自动服务器到服务器的过程重做它。但当我尝试激活此插件时遇到致命错误。

function remote_init() 
{
    openZip('http://myserver.com/upgrade.zip');
    $target = ABSPATH.'wp-content/themes/mytheme/';
}


function openZip($file_to_open, $debug = false) { 
    global $target;
    $file = realpath('/tmp/'.md5($file_to_open).'.zip');

//$file 始终为空。在这种情况下不能使用 realpath。该怎么办?

    $client = curl_init($file_to_open);
    curl_setopt(CURLOPT_RETURNTRANSFER, 1);

    $fileData = curl_exec($client);

    file_put_contents($file, $fileData);

    $zip = new ZipArchive();  
    $x = $zip->open($file);  
    if($x === true) {  
        $zip->extractTo($target);  
        $zip->close();  

        unlink($file);  
    } else {
        if($debug !== true) {
            unlink($file);
        }  
        die("There was a problem. Please try again!");  
    }  
} 


add_action( 'init','remote_init');

我快速检查了一下手册,发现第 5 行有一个小错误。

$target = ABSPATH .'wp-content/themes/mytheme/';
function openZip($file_to_open, $debug = false) { 
    global $target;
    $file = ABSPATH . '/tmp/'.md5($file_to_open).'.zip';
    $client = curl_init($file_to_open);
    curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);  //fixed this line

    $fileData = curl_exec($client);

    file_put_contents($file, $fileData);

    $zip = new ZipArchive();  
    $x = $zip->open($file);  
    if($x === true) {  
        $zip->extractTo($target);  
        $zip->close();  

        unlink($file);  
    } else {
        if($debug !== true) {
            unlink($file);
        }  
        die("There was a problem. Please try again!");  
    }  
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

服务器到服务器 > 检索远程 zip 文件并将其解压到本地服务器目录 的相关文章

随机推荐