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

时间:2015-11-18 17:37:08

标签: php bash shell exec unzip

所以我有这段代码:

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

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

1 个答案:

答案 0 :(得分:0)

shell_exec将返回解压缩的输出。

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

仅当存档正常时才会输出

unzip -tq archive.zip。您可以先运行此命令,检查PHP中的输出并在成功后运行提取。