Search code examples
phpbashshellexecunzip

Detect failure of shell exec unzip on PHP


so I have this code:

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

My question is, how do I reliably know in PHP whether or not the unzip operation succeeded or if it ended up encountering some error?


Solution

  • shell_exec will return the output of unzip.

    Adding -q parameter to unzip will suppress output, you can set to suppress all messages except errors and if $ret length not equal to 0 then the operation didn't complete successfully.

    unzip -tq archive.zip will output only if the archive is OK or not. You can run this command first, check the output in PHP and run the extraction afterwards if successful.