php shell_exec() out put to get a text file
I'm开发者_Go百科 trying to run rate -c 192.168.122.0/24
command on my Centos computer and write down the output of that command to the text file using shell_exec('rate -c 192.168.122.0/24')
command; still no luck!!
If you don't need PHP, you can just run that in the shell :
rate -c 192.168.122.0/24 > file.txt
If you have to run it from PHP :
shell_exec('rate -c 192.168.122.0/24 > file.txt');
The ">" character redirect the output of the command to a file.
You can also get the output via PHP, and then save it to a text file
$output = shell_exec('rate -c 192.168.122.0/24');
$fh = fopen('output.txt','w');
fwrite($fh,$output);
fclose($fh);
As you forgot to mention, your command provides a non ending output stream. To read the output in real time, you need to use popen.
Example from PHP's website :
$handle = popen('/path/to/executable 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
You can read the process output just like a file.
$path_to_file = 'path/to/your/file';
$write_command = 'rate -c 192.168.122.0/24 >> '.$path_to_file;
shell_exec($write_command);
hopes this helps. :D And this will direct you to a good way. https://unix.stackexchange.com/a/127529/41966
精彩评论