Monday, 24 September 2018

Execute shell commands in PHP

Like any other language php applications often need to execute system commands like they are run from the terminal/console/commandline. Php has multiple functions to do this task. Lets take a look at each of these

1. system
"system() is just like the C version of the function in that it executes the given command and outputs the result.
The system() call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module."
"The passthru() function is similar to the exec() function in that it executes a command. This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser. A common use for this is to execute something like the pbmplus utilities that can output an image stream directly. By setting the Content-type to image/gif and then calling a pbmplus program to output a gif, you can create PHP scripts that output images directly."
3. exec
"exec() executes the given command."
"This function is identical to the backtick operator."

Portable solution

A common problem with the above functions is that they are often disabled selectively on shared hostings. For example all function expect passthru may be disabled , or only exec would be available and so on. So it creates a small issue to keep changing code to fit according to the hosting server.
Here is a function that combines all the above functions into 1 and creates a portable solution that tries to work on all kinds of shared hostings where atleast 1 of them is enabled :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
    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);
}
To use :
1
2
3
4
5
6
7
8
9
$o = terminal('ls');
if($status == 0)
{
    echo $o['output'];
}
else
{
    //some problem
}
system and passthru echo the output directly to output stream , hence its necessary to use buffering to catch their output before it gets dumped. shell_exec and exec simply return the output.
So using the terminal function your code is more portable across different kinds of hostings and lot of command execution issues can be avoided.

0 comments:

Post a Comment