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

Sco*_*t B 5 php

我在服务器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');
Run Code Online (Sandbox Code Playgroud)

// $ 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');
Run Code Online (Sandbox Code Playgroud)

Jon*_*nah 1

我快速查看了手册,发现第 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!");  
    }  
}
Run Code Online (Sandbox Code Playgroud)