我有一个问题,我在Linux服务器上安装'Archive_Zip 0.1.1',但当我试图运行脚本创建zip文件时,它给出了致命的错误

致命错误:类ZipArchive没有在…

我把密码放哪了

$zip = new ZipArchive;
var_dump($zip);
$res = $zip->open($filename, ZipArchive::OVERWRITE);
if ($res !== TRUE) {
    echo 'Error: Unable to create zip file';
    exit;
}
if (is_file($src)) {
    $zip->addFile($src);
} else {
    // echo "<br>" . dirname(__FILE__) . $src;//'/install1';
    if (!is_dir($src)) {
         $zip->close();
         @unlink($filename);
         echo 'Error: File not found';
         exit;
    }
    recurse_zip($src, $zip, $path_length);
}
$zip->close();
echo "<br>file name ".$filename;

但是它没有找到类文件。

请告诉我解决办法。我该怎么解决这个问题呢? 我还把php.ini文件放在脚本所在的文件夹中,但它不起作用。


当前回答

首先,远程服务器的解决方案:

如果你正在使用cpanel,你可能已经安装了zip扩展,但没有激活。你需要激活它。在这种情况下,你需要去cpanel >在软件部分>点击PHP版本。然后找到zip并检查它。现在保存。

你应该看到像图上那样。

刷新页面。错误应该会消失。

注意:如果您没有找到,请联系服务器提供商。他们会为你安装的。

其他回答

对于PHP 7.4 >,默认情况下不再存在ZipArchive,您需要安装和配置它。

我强烈建议你使用一个可以包含在你的项目中的库,比如https://github.com/zanysoft/laravel-zip 这个库只是原生ZipArchive的包装器

Centos 6

或者任何基于rhel的口味

yum install php-pecl-zip

service httpd restart

在ubuntu桌面上,我必须这么做。

sudo apt-get install php5.6-zip

这安装了库,但我仍然得到相同的错误,所以我必须重新启动apache使用:

sudo service apache2 restart

这招奏效了。

要出现ZipArchive类,PHP需要安装zip扩展。

有关安装说明(Linux和Windows),请参阅此页。

适用于Centos 7和Remi上的PHP 7.3

搜索zip扩展名:

$ yum search php73 | grep zip
php73-php-pecl-zip.x86_64 : A ZIP archive management extension

扩展名是php73-php-pecl-zip.x86_64。要在运行单一版本PHP的服务器上安装它,删除php73前缀:

$ sudo yum --enablerepo=remi-php73 install php-pecl-zip #for server running single PHP7.3 version
$ #sudo yum --enablerepo=remi-php73 install php73-php-pecl-zip # for server running multiple PHP versions

重启PHP:

$ sudo systemctl restart php-fpm

检查已安装的PHP扩展:

$ php -m
[PHP Modules]
apcu
bcmath
bz2
...
zip
zlib