W
W
whats2014-06-05 19:30:06
linux
whats, 2014-06-05 19:30:06

How do system() exec() and stuff work in PHP?

Good afternoon.
The question is the following - in php there are functions that execute system programs and display their output. There are many of them and in fact they work the same way. But here is one thing. Not a single installed program is executed through them. For example, I want to convert a file
exec('enconv filepath');
This doesn't work. I try to do the same through the console, everything works fine.
Tried to prescribe the full path to the binary.
/usr/bin/enconv
no difference. I also tried all the paths to the files that are possible. Nothing works.
I, of course, allowed the functions. I also tried to execute from the www-data user in the console - everything is done. And here through php the script does not want.
But, for example, calls

exec('echo 1');
exec('file -i filename');

calmly work out, and in the output I see the information.
Tell me, please, what could be the plug?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
whats, 2014-06-22
@whats

I would like to give a solution if anyone is interested.
If we want to emulate the console in Php with all its responses in Php, we should use this construction.

$cmd = 'enconv /var/www/documents/xxx/\(1\)-1402322554-file -x utf8 -L russian';
$process = proc_open($cmd,
        array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes, NULL, NULL)) {
            echo "<b>$cmd</b><br/>";
            echo "OUTPUT:".stream_get_contents($pipes[1])."<br/>";
            echo "ERRORS:".stream_get_contents($pipes[2])."<br/>";
            echo "<br/>";
        }

So we get all the information that the console returns to us. All errors are returned the same way. It is very convenient when writing a program that uses external program calls in the code.

S
Stepan, 2014-06-05
@L3n1n

The user who tries to run /usr/bin/enconv most likely does not have permission to do so.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question