Last active
September 15, 2016 22:10
-
-
Save kuroisuna/83195ec3dc108f3c3f3a17761b19c3c2 to your computer and use it in GitHub Desktop.
PHP: Executes a console command and returns success or failure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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