0

I have a C code that I have to execute through PHP, I have used exec('./sys'), sys is my executable file. I have also tried system(), passthrough(), shell_exec() and they are not giving output.

When I executed exec('who'); it gives the output.

What can I do to execute sys?

1
  • 2
    try exec('ls') or exec('pwd') to see where you are, and make sure sys is actually there Commented Nov 13, 2015 at 15:14

1 Answer 1

1

Each of those methods you reference will execute your sys file, but you need to make sure you are executing the correct path. Your working path is determined by what script is actually executing PHP. For example, if you're executing your code through apache or the command line your working directory may be different. Lets assume this file structure:

+ src/
| + script.php
| + sys

I would recommend using PHP's __DIR__ magic variable in your script.php to always reference the current file's directory, and then work from there:

// script.php
exec(__DIR__ . "/sys");

Retrieving output can be done a couple different ways. If you want to store the output of the script in a variable, I would recommend using exec according the the manual:

Parameters

command The command that will be executed.

output 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. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

return_var If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

exec will return the first line of output, but if you want more than that you need to pass a variable by reference:

// script.php
$output = array();
exec(__DIR__ . "/sys", $output);

$output will then contain an array of each line of output from the command. However if you want to run your sys script and directly pass through the output then use passthru(__DIR__ . "/sys"); For example, if you wanted to execute a command that required input on the command line, passthru would be the best option.

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

4 Comments

I tried $output = array(); exec(DIR . "/sys", $output); but out put is "array()"..i exicuted exec("ls",$output);, it gives me correct results......My c program contain a code for creating users for ubuntu system and generating public key for them.
.Cant i prompt the terminal when i exicuting php?
if i exicute exec("mkdir hi",$output); directory not creating...output is'' Array()''
are you trying to echo $output directly? You cannot do that in PHP. Try print_r($output);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.