1

I have a program that I can run on my command line but I was wondering if I could actually get it to run in php. Basically my program would have a user insert a couple values to search for, then those values would be passed on into the program for it to run. Then I would want the result of the program to be displayed

I found a function called exec() but I didn't understand it at all so I was wondering if anyone else knows a way or can help me out!

5
  • exec() is the function you're looking for. Pass what you need into it and as long as the web server has permissions it will execute the program. Commented May 2, 2015 at 20:56
  • can you write some aditional info about your system(windows, linux) and your program? How you run your program from the command line? Commented May 2, 2015 at 20:57
  • @Augwa yes, although exec() does the job, keep in mind security aspect. Commented May 2, 2015 at 21:33
  • @sitilge The same could be said about sql injection, or a php file that can read files :) It's up to the developer to correctly sanitize any user input before sending it to another process which will not sanitize. Commented May 2, 2015 at 21:57
  • @Augwa you are right, the exec was the subject in this particular case. Commented May 3, 2015 at 21:05

1 Answer 1

1

exec() runs a command on the command line, just as you desire. You can capture the output of the command in an array named as the second argument.

For example:

exec("whoami", $output);
var_dump($output);

This runs linux's "whoami" command and captures the result in the array $output. The second line displays the contents of the array. Is that similar to what you want to do?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.