Software Developer

Error Codes For system(“unzip archive.zip”) in PHP

If you’re using system("unzip archive.zip") to unzip an archive file in PHP, and you want to catch errors, you’ll notice that it only returns a number for the error code. Here are the error codes (exit status codes) and meanings for the unzip command.

This PHP array matches the unzip error code with its meaning. See example usage below.

$unzip_errors = array(
	1 => 'one or more warning errors were encountered, but processing completed successfully anyway. This includes zipfiles where one or more files was skipped due to unsupported compression method or encryption with an unknown password.',
	2 => 'a generic error in the zipfile format was detected. Processing may have completed successfully anyway; some broken zipfiles created by other archivers have simple work-arounds.',
	3 => 'a severe error in the zipfile format was detected. Processing probably failed immediately.',
	4 => 'unzip was unable to allocate memory for one or more buffers during program initialization.',
	5 => 'unzip was unable to allocate memory or unable to obtain a tty to read the decryption password(s).',
	6 => 'unzip was unable to allocate memory during decompression to disk.',
	7 => 'unzip was unable to allocate memory during in-memory decompression.',
	8 => '[currently not used]',
	9 => 'the specified zipfiles were not found.',
	10 => 'invalid options were specified on the command line.',
	11 => 'no matching files were found.',
	50 => 'the disk is (or was) full during extraction.',
	51 => 'the end of the ZIP archive was encountered prematurely.',
	80 => 'the user aborted unzip prematurely with control-C (or similar)',
	81 => 'testing or extraction of one or more files failed due to unsupported compression methods or unsupported decryption.',
	82 => 'no files were found due to bad decryption password(s). (If even one file is successfully processed, however, the exit status is 1.)'
);

Exit status ‘0’ is not included because ‘0’ means there were no errors, and unzip was successful.

Usage Example

The following is an example of using the unzip error codes to catch an error when unzipping a .zip file in PHP. If unzip fails with an error, the $message will have the appropriate error message for the error number.

(Note that I use ob_start() and ob_get_clean() in order to have system("unzip archive.zip") work silently without echoing.)

Mandatory edits: replace $destination and $zip_file with your own.

ob_start();
system("unzip -d $destination $zip_file", $return);
$result = ob_get_clean();

if ( 0 === $return ) {

	// SUCCESS

	$message = "The file '$zip_file' was successfully unzipped."


} else {
	$unzip_errors = array(
		1 => 'one or more warning errors were encountered, but processing completed successfully anyway. This includes zipfiles where one or more files was skipped due to unsupported compression method or encryption with an unknown password.',
		2 => 'a generic error in the zipfile format was detected. Processing may have completed successfully anyway; some broken zipfiles created by other archivers have simple work-arounds.',
		3 => 'a severe error in the zipfile format was detected. Processing probably failed immediately.',
		4 => 'unzip was unable to allocate memory for one or more buffers during program initialization.',
		5 => 'unzip was unable to allocate memory or unable to obtain a tty to read the decryption password(s).',
		6 => 'unzip was unable to allocate memory during decompression to disk.',
		7 => 'unzip was unable to allocate memory during in-memory decompression.',
		8 => '[currently not used]',
		9 => 'the specified zipfiles were not found.',
		10 => 'invalid options were specified on the command line.',
		11 => 'no matching files were found.',
		50 => 'the disk is (or was) full during extraction.',
		51 => 'the end of the ZIP archive was encountered prematurely.',
		80 => 'the user aborted unzip prematurely with control-C (or similar)',
		81 => 'testing or extraction of one or more files failed due to unsupported compression methods or unsupported decryption.',
		82 => 'no files were found due to bad decryption password(s). (If even one file is successfully processed, however, the exit status is 1.)'
	);

	// unzip ERROR

	$message = "unzip failed because: $unzip_errors[$return]";

}

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]