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');
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!");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With