tags:

views:

53

answers:

2

The purpose of this code is to pull upgrade.zip from a central server, extract it and place it in a folder on the resident server. I get no errors, it just results in the die("!There was a problem. Please try again!");

require('../../../wp-blog-header.php');

function openZip($file_to_open) { 
    global $target;  
    $zip = new ZipArchive();  
    $x = $zip->open($file_to_open);  
    if($x === true) {  
        $zip->extractTo($target);  
        $zip->close();  
        unlink($file_to_open);  
    } else {  
        die("!There was a problem. Please try again!");  
    }  
} 

$payload = file_get_contents('http://myserver.com/upgrade.zip');
if(isset($payload)) 
    {
    $filename = 'upgrade.zip';
    $source = file_get_contents('http://myserver.com/upgrade.zip');
    $target = ABSPATH.'wp-content/themes/mytheme/';

    // permission settings for newly created folders
    $chmod = 0755;  

    $saved_file_location = $target . $filename;

openZip($saved_file_location);

}
A: 

You should investigate the return value from $zip->open(). It may be any of the following:

ZIPARCHIVE::ER_EXISTS 
ZIPARCHIVE::ER_INCONS 
ZIPARCHIVE::ER_INVAL 
ZIPARCHIVE::ER_MEMORY 
ZIPARCHIVE::ER_NOENT 
ZIPARCHIVE::ER_NOZIP 
ZIPARCHIVE::ER_OPEN 
ZIPARCHIVE::ER_READ 
ZIPARCHIVE::ER_SEEK 

Also, why not try to open the downloaded zip-file manually using your favourite unzip program to check if the file is indeed valid?

Martin Wickman
+1  A: 

You get the contents of the remote zip file into a string... but you never save it anywhere.

Ignacio Vazquez-Abrams
This is the problem - you need to write out the contents of the zip file into ABSPATH.'wp-content/themes/mytheme/upgrade.zip' - right now you're just keeping the file in memory - it doesn't actually exist on-disk.
Andy Shellam
@ignacio - I'm only familiar with move_uploaded_file($source, $saved_file_location) for example. How would I save the file from file_get_contents?
Scott B
With `fopen()`, `fwrite()`, and `fclose()`.
Ignacio Vazquez-Abrams
@Andy > Thanks for the help. I don't see how to specify the destination of the fwrite(). For example, I can use $myfile = fwrite($target); then send $myfile to the openZip? openZip($myfile)?
Scott B
@Scott - file_put_contents might be a better option - I believe it's a wrapper for fopen(), fwrite() and fclose() - you just pass it a filename, and your data downloaded from the remote server, then pass the same filename to your open zip method.
Andy Shellam