PHP 类“ZipArchive”在浏览器中工作正常,但在 CLI 模式下找不到
所以我有这个脚本 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PHP CLI 可能具有与“浏览器”版本所使用的不同的 php.ini,因此可能会发生在一个版本中启用扩展而在另一个版本中未启用的情况。我知道几年前 WAMP 就是这样,所以 XAMPP 也可能是这样。检查每个版本加载了哪些 ini 文件:
对于浏览器版本:
对于 PHP CLI:
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:
For PHP CLI: