Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server to Server > Retrieve and extract a remote zip file to local server directory

Tags:

php

I have a wp plugin file on server B who's purpose is to retrieve a zip file from a remote server A.

Once Server B receives the zip file, it should extract the contents and copy the files into a specific folder on server B overwriting any existing files.

I have some code below that I've borrowed from a file that uses and uploader to do the same thing and I'd just like to redo it for the automated server to server procedure described above. But I'm getting a fatal error when trying to Activate this plugin.

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 is always empty. can't use realpath in this case. What to do?

    $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');
like image 673
Scott B Avatar asked Nov 14 '22 12:11

Scott B


1 Answers

I did a quick check in the manual, and there was a slight error on line 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!");  
    }  
}
like image 121
Jonah Avatar answered Dec 25 '22 13:12

Jonah