Skip to content

Instantly share code, notes, and snippets.

@kuroisuna
Last active September 15, 2016 22:10
Show Gist options
  • Save kuroisuna/83195ec3dc108f3c3f3a17761b19c3c2 to your computer and use it in GitHub Desktop.
Save kuroisuna/83195ec3dc108f3c3f3a17761b19c3c2 to your computer and use it in GitHub Desktop.
PHP: Executes a console command and returns success or failure
<?php
/**
* Validates a .zip file with unzip
* @param string $filePath
* @return boolean
*/
function validate($filePath)
{
// File is valid
if (!is_file($filePath)) {
die('File doesn\'t exists or is invalid');
}
/* The command redirects the output to /dev/null because
we don't really need it in this case.
The important part is the "echo $?" that will display an integer greater than zero
if there was an error in the last command.
If we wanted the actual text representation of the error
thrown by the command, then we may log the output to a temp file
and retrieve it after. */
$cmd = "unzip -qqt '{$filePath}' > /dev/null; echo $?";
// shell_exec returns the output
$isInvalid = (int) shell_exec($cmd);
if ($isInvalid) {
die("This ZIP file is invalid or has errors");
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment