Skip to content

Instantly share code, notes, and snippets.

@banago
Created April 11, 2012 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banago/2361493 to your computer and use it in GitHub Desktop.
Save banago/2361493 to your computer and use it in GitHub Desktop.
PHP: Execute a command in the terminal
<?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