tags:

views:

211

answers:

2

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');
A: 
function openZip($file_to_open, $debug = false) { 
    global $target;
    $file = realpath('/tmp/'.md5($file_to_open).'.zip');
    $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!");  
    }  
}  
prodigitalson
@prodigitalson > Thanks for the answer. I'm trying to integrate it into the overall solution, but I'm still getting a fatal error. I've edited the original question with my code after your answer. Can you have a look at the remote_init() bits and tell me what you think might be the problem?
Scott B
@Scot B > prodigitalson put a cURL call into the `openZIP()` function. I think you're supposed to use `openZIP()` with the remove file (myserver.com/upgrade.zip) as the `$file_to_open argument`.
Jonah Bron
Thanks for the help Jonah. I'm still getting an error: Warning: Wrong parameter count for curl_setopt()I've edited the code in the original question to reflect your suggestion. Any ideas?
Scott B
A quick look at the PHP manual shows a slight typo. Line 5 should be this instead: `curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);`
Jonah Bron
A: 

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!");  
    }  
}
Jonah Bron
Getting there. One less error. Now we have: Warning: file_put_contents() [function.file-put-contents]: Filename cannot be empty. Also, where do I set the value for $target? In the remote_init() function? $target = ABSPATH.'wp-content/themes/mytheme/';
Scott B
I changed the code above. Try it again. ABSPATH is defined, right?
Jonah Bron
Jonah, I really appreciate you sticking with me on this. ABSPATH is defined, however, it appears to be wrong...echo($target) returns: /home/myaccount/public_html/mysiteaddress//tmp/47393ca1964f2997cdee4b500721aca8.zip
Scott B
On this server, the tmp directory path should be.../home/myaccount/tmp
Scott B
Then you will have to change the code that defines ABSPATH to what you just gave (/home/myaccount). Then when $target appends "/tmp/md5(blahblah).zip", it will come out as /home/myaccount/tmp/47393ca1964f2997cdee4b500721aca8.zip.
Jonah Bron