PHP 类“ZipArchive”在浏览器中工作正常,但在 CLI 模式下找不到

发布于 2025-01-16 15:28:39 字数 582 浏览 4 评论 0原文

所以我有这个脚本 example.php:

<?php
$zip = new \ZipArchive();

如果我从浏览器调用它,它运行良好,没有任何错误,但如果我像这样在 CLI 模式下运行它(从另一个 php 文件):

<?php
echo system("php example.php");

我将看到这个输出:致命错误:未捕获错误:在 /Applications/XAMPP/xamppfiles/htdocs/... 中找不到类“ZipArchive”...

我使用这样的脚本,因为我需要调用它们并在无人值守的情况下运行,并且它们工作正常,直到我需要到包括 ZipArchive 类。

为什么在 cli 模式下找不到 ZipArchive 类?

我提到,如果我从终端 php example.php 运行,它也可以正常工作。

我已经花了几个小时试图弄清楚,这让我发疯。非常感谢您的帮助。

本地服务器:XAMPP、PHP 版本 7.4.25、macOS Big Sur

So I have this script example.php:

<?php
$zip = new \ZipArchive();

if I call it from a browser it runs fine without any errors, but if I run it in CLI mode like this (from another php file):

<?php
echo system("php example.php");

I will see this output: Fatal error: Uncaught Error: Class 'ZipArchive' not found in /Applications/XAMPP/xamppfiles/htdocs/...

I use scripts like this because I need to call them and run unattended and they are working fine until I needed to include the ZipArchive class.

Why it can't find the ZipArchive class in cli mode?

I mention that if I run from terminal php example.php it is also working fine.

I spend several hours already trying to figure out, it is driving me crazy. Your help is really appreciated.

local server: XAMPP, PHP Version 7.4.25, macOS Big Sur

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

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

发布评论

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

评论(1

末蓝 2025-01-23 15:28:39

PHP CLI 可能具有与“浏览器”版本所使用的不同的 php.ini,因此可能会发生在一个版本中启用扩展而在另一个版本中未启用的情况。我知道几年前 WAMP 就是这样,所以 XAMPP 也可能是这样。检查每个版本加载了哪些 ini 文件:

对于浏览器版本:

<?php phpinfo() ?>

对于 PHP CLI:

php --ini

PHP CLI might have a different php.ini from what the "browser" version is using, so it might happen that an extension is enabled in one, but not in the other. I know this was the case in WAMP several years ago, so it might be the same in XAMPP too. Check what ini files are loaded by each version:

For the browser version:

<?php phpinfo() ?>

For PHP CLI:

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