A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearPHP - exec() vs system() vs passthru() - page 1
User InfoPosts
PHP - exec() vs system() vs passthru()#1
What are the differences?

Is there a specific situation or reason for each function? If yes, can you give some examples of those situations?

PHP.net says that they are used to execute external programs. see reference
From the examples I see, I don(t see any obvious difference.

If I were to simply run a script (bash or python), which function do you recommend me to use?

posted date: 2009-04-08 20:44:00

Re: PHP - exec() vs system() vs passthru()#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-08 20:44:01

Re: PHP - exec() vs system() vs passthru()#3
They have slightly different purposes.exec() is for calling a system command, and perhaps dealing with the output yourself.system() is for executing a system command and immediately displaying the output - presumably text.passthru() is for executing a system command which you wish the raw return from - presumably something binary.Regardless, I suggest you not use any of them. They all produce highly unportable code.

posted date: 2009-04-08 20:50:00

Re: PHP - exec() vs system() vs passthru()#4
The system() FunctionThe system function in PHP takes a string argument with the command to execute as well as any arguments you wish passed to that command. This function executes the specified command, and dumps any resulting text to the output stream (either the HTTP output in a web server situation, or the console if you are running PHP as a command line tool). The return of this function is the last line of output from the program, if it emits text output.The exec() FunctionThe system function is quite useful and powerful, but one of the biggest problems with it is that all resulting text from the program goes directly to the output stream. There will be situations where you might like to format the resulting text and display it in some different way, or not display it at all.For this, the exec function in PHP is perfectly adapted. Instead of automatically dumping all text generated by the program being executed to the output stream, it gives you the opportunity to put this text in an array returned in the second parameter to the function:The shell_exec() FunctionMost of the programs we have been executing thus far have been, more or less, real programs1. However, the environment in which Windows and Unix users operate is actually much richer than this. Windows users have the option of using the Windows Command Prompt program, cmd.exe This program is known as a command shell.The passthru() FunctionOne fascinating function that PHP provides similar to those we have seen so far is the passthru function. This function, like the others, executes the program you tell it to. However, it then proceeds to immediately send the raw output from this program to the output stream with which PHP is currently working (i.e. either HTTP in a web server scenario, or the shell in a command line version of PHP).The proc_open() Function and popen() functionproc_open() is similar to popen() but provides a much greater degree of control over the program execution. cmd is the command to be executed by the shell. descriptorspec is an indexed array where the key represents the descriptor number and the value represents how PHP will pass that descriptor to the child process. pipes will be set to an indexed array of file pointers that correspond to PHP(s end of any pipes that are created. The return value is a resource representing the process; you should free it using proc_close() when you are finished with it.Credits: http://php.net/ && Chipmunkninja

posted date: 2009-04-08 20:51:00

Re: PHP - exec() vs system() vs passthru()#5
Sometimes portability has to be sacrificed for functionality. There are some things PHP just can't do well.

posted date: 2009-04-08 21:10:00

Re: PHP - exec() vs system() vs passthru()#6
Granted, but 95% of the time people want to use a shell call, the reasons are all wrong.

posted date: 2009-04-08 22:39:00

Re: PHP - exec() vs system() vs passthru()#7
@Kalium: can you elaborate more on your statement? just stating some vague percentage statistics does not convince me. I believe that using system calls to execute scripts are totally fine as long as the whole application does not depend one a bunch of scripts in the back-end.

posted date: 2009-04-08 23:01:00

Re: PHP - exec() vs system() vs passthru()#8
If you(re running your PHP script from the command-line, passthru() has one large benefit. It will let you execute scripts/programs such as vim, dialog, etc, letting those programs handle control and returning to your script only when they are done.If you use system() or exec() to execute those scripts/programs, it simply won(t work.Gotcha: For some reason, you can(t execute less with passthru() in PHP.

posted date: 2009-04-09 09:13:00

Re: PHP - exec() vs system() vs passthru()#9
Uhm WHAT? How is it unportable? dir, for instance, works on all OSes. It isn't unportable it just takes some effort to make it portable. -1 for this silly notion.

posted date: 2011-09-18 01:59:00

Re: PHP - exec() vs system() vs passthru()#10
I don't understand what you're saying. You can execute programs both from CLI and (F)CGI (as well as mod_php). There may be system-imposed restrictions, such as selinux. But a well set-up system will have those selectively off. Of course a shared host is a different story, but you won't offer a shared environment to esteemed clients either, no?

posted date: 2011-09-18 02:01:00

Re: PHP - exec() vs system() vs passthru()#11
There's also proc_open() and popen(), both of which allow a higher degree of control over the spawned process.

posted date: 2011-09-18 02:03:00

Re: PHP - exec() vs system() vs passthru()#12
@Christian izkata@izein:~$ dir -bash: dir: command not found - FreeBSD

posted date: 2012-04-10 11:05:00

select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.