远程 Zip 提取 >似乎无法到达终点线

发布于 2024-08-23 08:45:14 字数 1319 浏览 4 评论 0原文

此代码的目的是从远程服务器获取 update.zip 文件,将其解压缩并将其保存到本地目录,更新、覆盖或创建更新的文件。

我几乎已经有了这个工作的非 cURL 版本,但我宁愿使用这个版本。我遇到的第一个问题是 tmp 文件夹的路径不正确。我需要一种更好的方法来嗅探它(暂时硬编码)......

第二个问题是代码不起作用,但它没有抛出错误。它执行 $x 分支,但没有进行 zip 提取。

require('../../../wp-blog-header.php'); //enables wp security check and ABSPATH

    $payload = file_get_contents('http://myserver.com/upgrade.zip'); //grab the file from the remote server
    $target = ABSPATH .'wp-content/themes/mytheme/'; // this is the destination for the unzipped files

openZip($payload); 

function openZip($file_to_open, $debug = false) { 
    global $target;
    $file = ABSPATH . '/tmp/'.md5($file_to_open).'.zip'; //this should be home/myfolder/tmp but ABSPATH is giving the wrong path to the tmp directory.
    $client = curl_init($file_to_open);
    curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);  
    $fileData = curl_exec($client);

    file_put_contents($file, $fileData);

    $zip = new ZipArchive();  
    $x = $zip->open($file);  
    if($x === true) {  //this is true, but no zip extraction?
        $zip->extractTo($target);  
        $zip->close();  

        unlink($file);  
    } else {
        if($debug !== true) {
            unlink($file);
        }  
        die("There was a problem. Please try again!");  
    }  
}

The purpose of this code is to grab an update.zip file from a remote server, unzip it and save it to a local directory, updating, overwriting or creating the updated files.

I've almost got a non cURL version of this working, but I'd rather use this version. The first problem I have is that the path to the tmp folder is incorrect. I need a better method of sniffing that out (temporarily hardcoded)...

The 2nd problem is that the code's not working, but its not throwing an error. Its executing the $x branch but no zip extraction is taking place.

require('../../../wp-blog-header.php'); //enables wp security check and ABSPATH

    $payload = file_get_contents('http://myserver.com/upgrade.zip'); //grab the file from the remote server
    $target = ABSPATH .'wp-content/themes/mytheme/'; // this is the destination for the unzipped files

openZip($payload); 

function openZip($file_to_open, $debug = false) { 
    global $target;
    $file = ABSPATH . '/tmp/'.md5($file_to_open).'.zip'; //this should be home/myfolder/tmp but ABSPATH is giving the wrong path to the tmp directory.
    $client = curl_init($file_to_open);
    curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);  
    $fileData = curl_exec($client);

    file_put_contents($file, $fileData);

    $zip = new ZipArchive();  
    $x = $zip->open($file);  
    if($x === true) {  //this is true, but no zip extraction?
        $zip->extractTo($target);  
        $zip->close();  

        unlink($file);  
    } else {
        if($debug !== true) {
            unlink($file);
        }  
        die("There was a problem. Please try again!");  
    }  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅倚清风 2024-08-30 08:45:14

最可能的原因是您实际上并未在 openZip 内的 file_put_contents() 调用中写入 zip 文件数据。如果您将零长度文件传递给 $zip->open(),它会很乐意返回 (bool)true,即使您显然无法从中提取任何内容它。请参阅此示例

The most likely cause here is that you're not actually writing the zip file data in your file_put_contents() call inside openZip. If you pass a zero-length file to $zip->open(), it will happily go about its way returning (bool)true, even though you obviously will not be able to extract anything from it. See this example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文