Go Back   Webmaster Forums UK SEO SEM Webmaster Community Forum - UKWW > Web Design and Website Development > Programming > PHP Forum
Register FAQ Members List Downloads Calendar Today's Posts Webmaster Resources Webmaster Blogs
 
 

PHP Forum PHP related discussion, help, tips and tutorials.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-02-2008, 07:19 PM
dman_2007
Guest
 
Posts: n/a
Default Tip : running external programs / commands from php script

If you ever wanted to run an external command / program from inside your PHP script, PHP provides following ways to do it :

1) backticks operator

You can use `` operators. Note that these are not same as apostrophes (single quotes) ''. Generally backtick is located on the key as ~ (tilde). Whenever you use backticks operator, PHP will try to run the content contained within backticks as a shell command from current directory and the command output will be returned which can be collected by using an assignment operator. For example, after running following code :

Code:
 $file_list  = `ls -l`;
$file_list variable will contain the complete command output.

2) exec function

exec function can also be used for running external commands. We pass the shell command as first parameter. We also need to pass two more variable references as parameters, exec function fills them up with command output and command return status value respectively. Variable containing program output is an array which contains each line of command output as separate array element. The exec function itself returns the last line of command output. For example, when we run this code :

Code:
 $last_line =  exec('ls -l', $file_list, $return_code);
$last_line variable will contain last line of command output. $file_list variable will be an array containing each line of command output as a separate element. $return_code variable will contain command return status value.

3) shell_exec function

shell_exec is similar to backticks operator, we need to pass it the external command as a parameter and it will return the complete command output. For example, after this code

PHP Code:
 $output shell_exec('ls -l'); 
$output variable will contain the complete command output.

4) system function

System function also takes the shell command to be run as its first parameter. Second parameter is a variable which passed as a reference to be filled up with command's return status value. Its different from shell_exec in the way it handles the command output. Instead of returning the complete command outoput, it only returns the last line of output. Another thing to note is that it autmatically sends the complete command output to the browser as well. For example, after running this code

Code:
$last_line = system('ls -l', $return_value);
$last_line variable will contain last line of command output. $return_value variable will contain command return status value. Complete command output will also be send to the browser by system command.

5) passthru function

passthru function is quite similar system function. It takes two parameters, first is the shell command to execute and second is a variable passed as reference used for collecting command return status value. passthru function doesn't returns any value. It sends the complete command output to the browser. This function is preferred over exec and system functions when running command which produce binary data output.


In my next post i will show you some other advanced methods as well. I will also discuss best practices for using these functions.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 04-03-2008, 01:28 PM
dman_2007
Guest
 
Posts: n/a
Default

The functions i discussed in my previous post and backticks operators are good when you want to run an external program just to get the output produced by the external program and the input required by the command can be supplied by using comman line parameters. But if you want to run an external program interactively, then you need to use following functions :

1) proc_open function

proc_open function takes three compulsory parameters. First parameter is the name of the external command to run. Second is an array which contains specifications of file descriptors, that is how do you want to connect with the external command's stdin, stdout, and stderr. Index 0 has specification for stdin, 1 for stdout and 2 for stderr. You can also specify more file descriptors which will be passed to external command and can be used for communication. The two pipe types which you can use are pipe and file. Third variable is passed as a reference which is set to be an array of file pointers corresponding to the descriptors specified in the second parameter. Optionally, you can also provide cwd, env and other options. For example, after running the following code :

Code:
<?php

 $proc = proc_open('wc', 
                   array(
                    0 => array('pipe', 'r'),
                    1 => array('pipe', 'w'),
                    2 => array('file', 'temp/log.txt', 'a')),
                   $fp_array);
  
 $input = <<<EDT
This is first line.
This is second line.
EDT;
 
 fwrite($fp_array[0], $input);
 fclose($fp_array[0]);
  
 $output = stream_get_contents($fp_array[1]);
 
 fclose($fp_array[1]); 
 proc_close($proc);
 
 echo $output;               
  
?>
we will get following output :

Quote:
1 8 40
Notice how we provided input to the external command by writing data to the stdin pipe of the external command and retrived command output by reading data from the stdout pipe of the external command. Be careful to close down connecting pipe file pointers before closing the process with proc_close function, otherwise a deadlock will be created and php will hang.

2) popen function

popen function also enables you to execute external program but if you use this function to run an external command then communication with the external command is unidirectional. That is you either write to external command's stdin or read from external command's stdout. popen function accepts two parameters. First parameter is the name of the external command to run and second is the mode of communication. For example, the following code will display the list of files in curent directory :

Code:
<?php

 $fp = popen('ls -l', 'r');
 $output = stream_get_contents($fp);
 pclose($fp);
 
 echo $output;               
  
?>
Notice how we used the pipe file pointer returned by popen to read the command output. Once the output is read, we close it with pclose function.

Last edited by dman_2007; 04-04-2008 at 01:28 PM.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #3 (permalink)  
Old 04-04-2008, 02:19 PM
dman_2007
Guest
 
Posts: n/a
Default

You can also use any one of these functions (except popen function) and backticks operator to run external progam which will run in background. To do it redirect standard input and standard stream to a file and send the command to background. For example,

Code:
<?php

  shell_exec('./test_command > /dev/null 2> /dev/null &');
  
  echo 'Command executed successfully!';   
  
?>
this code will execute external command test_command in the background and its stdout and stderr is redirected to /dev/null.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #4 (permalink)  
Old 04-05-2008, 07:34 AM
dman_2007
Guest
 
Posts: n/a
Default

Security

Now, while using these functions or backticks operator, best thing to do would be to not pass any input taken from the user using command line parameters. But if you can't avoid it then you should using following two functions to make sure that the user giving input doesn't tricks shell into executing arbitrary commands:

1) escapeshellarg function

This function is used for escaping single arguments coming from user input. It puts single quotes around strings and escapes any existing single quotes in the string allowing you to safely pass a string to the shell command.

2) escapeshellcmd function

This function is used for escaping meta characters in the complete command. On *nix based platform following characters are escaped : #&;`|*?~<>^()[]{}$\, \x0A and \xFF. single quote and double quotes are escaped only if they are not present in pairs. On windows platform, preceding characters alongiwht % are replaced by space.

If you have safe_mode option enabled in php.ini, then command strings are automatically escaped with escapeshellcmd function.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Webmaster Resources
UK WW SEO Tools
Find UK Hosts
 
The Forum Rules
Forum Rules - MUST READ
 
Site Of the Month
BizzFace
Nominate site of the month
 
Tag Cloud
43. wholesale adsense ready apple iphone 16gb apple iphone 16gb 3g articles article writer australia web hosting beauty cash casino cheap clothes communications content custard media database dgital camerals domain name english teacher fantasy football fantasy football league fittness football league free handbags home income instant jewelry links link sale medical health money money making online music natural health news nokia n96 16gb one way links online online shop poker professor quality links replica sam allcock seo social networking sony vaio laptop sunglasses technology the medical plus themes tutor verbalized wallet wallets wanted web hosting widget ready wordpress xmas offer

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:40 AM.

UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons.
Subscribe to our feeds   Subscribe to our feeds

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0