简体   繁体   English

PHP 5.3 ZipArchive问题

[英]PHP 5.3 ZipArchive issue

I have 5 accounts hosted at the same hosting plan, to each account I load my PHP CMS, hand made. 我有5个帐户以相同的托管计划托管,向每个帐户加载手工制作的PHP CMS。

I Upload a CMS.zip and an UnZip.php file, the entire system works fine as long as the server is configured to work with PHP 5.2. 我上传了CMS.zipUnZip.php文件,只要服务器配置为与PHP 5.2一起使用,整个系统就可以正常工作。

Recently I encountered an issue with my latest account configured as PHP 5.3, the CMS code is the same, But does not work, after a long and exhausting conversation with the support, they resolved that my code is set to extract only with PHP 5.2 not with 5.3. 最近,我遇到了一个问题,我的最新帐户配置为PHP 5.3,CMS代码相同,但是不起作用,在与支持人员进行了长时间的详尽讨论之后,他们解决了将我的代码设置为仅使用PHP 5.2提取的问题与5.3。

I use a standard PHP ZipArchive Class to handle extractions, which is why I found this weird, in any case I searched for a proper handle to un zip with PHP 5.3 but with no luck. 我使用标准的PHP ZipArchive类来处理提取,这就是为什么我觉得这个怪异的原因,无论如何,我都在寻找合适的句柄来用PHP 5.3解压缩,但是没有运气。

Can someone please tell me how this code, attached to this question, should be written? 有人可以告诉我该问题所附的代码应如何编写吗? Ps the support mentioned that the ZipArchive class is in fact active on the server. ps支持提到ZipArchive类实际上在服务器上处于活动状态。

Here is my code: 这是我的代码:

   $DIR_NAME = getcwd();

    $zip = new ZipArchive;
    if ($zip->open('ZipTest.zip') === TRUE) {
        $zip->extractTo($DIR_NAME);
        $zip->close();

        if(file_exists('editor.php')){
            echo '<u style="color:Green;">File Found ::   Extraction Complete !!</u><br>';
            echo $DIR_NAME.' : 
        <b style="color:orange;">ZipTest.zip Activated Seccessfully!</b> <a href="dir_inventory.php">Load CMS</a><br>';
        }
        else {
            echo '<u style="color:red;"><b> ERORR </b>::   Extraction Failure !!</u><br>';

        }
    } 
    else {
        echo '<b style="color:red;">Cant Exctract ZipTest.zip file to location!</b>';
    }

Here is my class to work with Zip. 这是我与Zip合作的课程。

I have no trouble with it on PHP 5.3.8. 我在PHP 5.3.8上没有问题。

<?php

/**
 *  @author         Andrew Zavadsky <zaffaccky@gmail.com>
 *  @class          Zip
 *  @description    Class for manipulation ZIP (creating, unzipping)
 */

class Zip
{
    private $zip            = null;

    public function __construct($src)
    {
        $this->zip  = new ZipArchive();

        if (!file_exists($src))
            $r = $this->zip->open($src, ZipArchive::CREATE);
        else
            $r = $this->zip->open($src);
    }

    public function addFromString($as, $text)
    {
        $this->zip->addFromString($as, $text);

        return $this;
    }

    public function addFile($as, $src = null)
    {
        if ($src && file_exists($src))
            $this->zip->addFile($src, $as);
        else
            $this->zip->addFromString($as, '');

        return $this;
    }

    public function unzip($to, array $files = array())
    {
        if (!file_exists($to))
            mkdir($to);

        if (!is_writable($to) || !is_dir($to))
            throw new Exception(sprintf('Target directory %s is not directory or not for writing', $to));

        if (count($files))
            $this->zip->extractTo($to, $files);
        else
            $this->zip->extractTo($to);

        $this->zip->close();

        return $this;
    }

    public function addFolder($as, $src = null)
    {
        $as = trim($as, '\\/');

        $this->zip->addEmptyDir($as);

        if ($src && file_exists($src) && is_dir($src))
            $this->addFiles($src, $as);

        return $this;
    }

    private function addFiles($from, $to = '', $recursive = true)
    {
        $from   = rtrim($from, '\\/');
        $to     = trim($to, '\\/');

        if (!is_dir($from))
            throw new Exception(sprintf('Can\'t add files from folder %s', $from));
        else
            $this->zip->addEmptyDir($to);


        foreach (scandir($from) as $i => $f)
        {
            if ($i <= 1)
                continue;

            if (is_dir($file = $from . DIRECTORY_SEPARATOR . $f) && $recursive)
                $this->addFiles($file, $to . DIRECTORY_SEPARATOR . $f, $recursive);
            else
                $this->addFile($to ? $to . DIRECTORY_SEPARATOR . $f : $f, $file);
        }

        return $this;
    }
}

Try it, you need $zip->unzip($to, $options); 试试看,您需要$ zip-> unzip($ to,$ options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM