Created
April 11, 2012 19:09
-
-
Save banago/2361493 to your computer and use it in GitHub Desktop.
PHP: Execute a command in the terminal
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 | |
/** | |
@author: http://www.binarytides.com/blog/40-techniques-to-enhance-your-php-code-part-3/ | |
Method to execute a command in the terminal | |
Uses : | |
1. system | |
2. passthru | |
3. exec | |
4. shell_exec | |
*/ | |
function terminal($command) | |
{ | |
//system | |
if(function_exists('system')) | |
{ | |
ob_start(); | |
system($command , $return_var); | |
$output = ob_get_contents(); | |
ob_end_clean(); | |
} | |
//passthru | |
else if(function_exists('passthru')) | |
{ | |
ob_start(); | |
passthru($command , $return_var); | |
$output = ob_get_contents(); | |
ob_end_clean(); | |
} | |
//exec | |
else if(function_exists('exec')) | |
{ | |
exec($command , $output , $return_var); | |
$output = implode("\n" , $output); | |
} | |
//shell_exec | |
else if(function_exists('shell_exec')) | |
{ | |
$output = shell_exec($command) ; | |
} | |
else | |
{ | |
$output = 'Command execution not possible on this system'; | |
$return_var = 1; | |
} | |
return array('output' => $output , 'status' => $return_var); | |
} | |
terminal('ls'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment