views:

94

answers:

3

like func("rm -rf /usr/local/)

Which 'func' will you use to do that?

+2  A: 

You would use PHP's exec function. Other options include system, passthru, and the backtick operator.

Thomas Owens
Why dont you use other similar function,any reason for this preference?
Shore
There's no single right answer as to always use one over the others. Personally, I try to avoid them since they aren't cross platform. In fact, depending on permissions and configurations, they might not always work on the same platform.
Thomas Owens
I used it to do this,but failed:exec("rm -rf F:/tmpJob/*").I can do it in command line myself.
Shore
That might be because the user that is running the PHP script doesn't have the permissions to execute the command.
Thomas Owens
How to overcome this most efficiently?
Shore
A: 

Personally I think proc_open is the best approach.

http://php.net/proc%5Fopen

This gives you far more control over input, output and let's you check it's exit status and STDOUT, which is more than most other commands for system calls. It also doesn't hold up the system while the command exits.

scragar
+4  A: 

It depends on what you want to obtain.

If you want to display the ouput of the command that has been executed, passthru does the trick :

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.

If you want to get the output of the command in a variable as a whole string, you can use shell_exec (which is the same as using the backtick operator) :

Execute command via shell and return the complete output as a string

And, if you want to get it line by line, you'll use exec :

If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n, is not included in this array.

And, finally, if you need a bit more control, you'll use proc_open -- see the documentation, about that one ; it's a bit more tricky, I guess.


Still, when using those :

  • Don't forget you are calling a program that has to be available on the host machine
    • This means if you are using a Linux command, your application will most likely not work on windows
  • Depending on the configuration of PHP, it is possible that only a couple of commands could be available ; see safe_mode_exec_dir, about that
  • Using this means launching another processus -- probably not that good for performances

So, I'd use those only when it is really necessary.

Pascal MARTIN
How to check apache user in windows?I checked httpd.conf and see it's daemon,but there is no such user on windows.How to find the "real" user?
Shore