php shell_exec()放入以获得文本文件

| 我试图在Centos计算机上运行
rate -c 192.168.122.0/24
命令,并使用
shell_exec(\'rate -c 192.168.122.0/24\')
命令将该命令的输出记录到文本文件中;仍然没有运气!
已邀请:
正如您忘记提到的那样,您的命令提供了一个无休止的输出流。要实时读取输出,您需要使用popen。 来自PHP网站的示例:
$handle = popen(\'/path/to/executable 2>&1\', \'r\');
echo \"\'$handle\'; \" . gettype($handle) . \"\\n\";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
您可以像读取文件一样读取过程输出。
如果您不需要PHP,则可以在shell中运行它:
rate -c 192.168.122.0/24 > file.txt
如果必须从PHP运行它:
shell_exec(\'rate -c 192.168.122.0/24 > file.txt\');
\“> \”字符将命令的输出重定向到文件。
您也可以通过PHP获取输出,然后将其保存到文本文件中
    $output = shell_exec(\'rate -c 192.168.122.0/24\');
    $fh = fopen(\'output.txt\',\'w\');
    fwrite($fh,$output);
    fclose($fh);
$path_to_file = \'path/to/your/file\';
$write_command = \'rate -c 192.168.122.0/24 >> \'.$path_to_file;
shell_exec($write_command);
希望这会有所帮助。 :D 这将引导您找到一个好方法。 https://unix.stackexchange.com/a/127529/41966

要回复问题请先登录注册