在PHP上检测shell exec解压缩失败


Detect failure of shell exec unzip on PHP

所以我有这个代码:

$destination = "/dest";
$filepath = 'filepath.zip';
$ret = shell_exec("unzip " . escapeshellarg($filepath) . " -d " . escapeshellarg($destination) . ";

我的问题是,我如何在PHP中可靠地知道解压缩操作是否成功,或者是否最终遇到了一些错误?

shell_exec将返回unzip的输出。

在解压中添加-q参数将抑制输出,您可以设置为抑制除错误之外的所有消息,如果$ret长度不等于0,则操作未成功完成。

仅当存档正常或不正常时,unzip -tq archive.zip才会输出。您可以先运行这个命令,在PHP中检查输出,如果成功,然后运行提取。