PHP shell_exec 无法使用 grep 和管道

PHP shell_exec not working with grep and pipes

我一直在尝试让这个 shell_exec() 命令在我的 PHP 脚本中工作,但它始终 returns 没有任何效果。我从命令行成功 运行 它并获得了预期的结果。

有人可以帮忙调试吗?

$cmd ="/bin/grep \"John Smith\" -oPr --include=\"*.txt\" --exclude=\"title.txt\"|/usr/bin/sort|/usr/bin/uniq -c";
$rs = exec($cmd);
echo "Command: $cmd<br/>";
echo "[$rs]<br/>";

非常感谢!

替换

--exclude=\"title.txt\"|

来自

--exclude=\"title.txt\" <DIRECTORY_TO_SEARCH> |

exec() 出于安全原因已在您的服务器上禁用 当PHP在安全模式下为运行时,此功能被禁用。

要在 php 中启用 exec()

如果您在 wamp 服务器上工作

停止 wamp 服务器 打开 php.ini 文件检查 disable_functions 通过在 disable_functions

之前添加分号 (;) 来评论 disable_functions

见下图

; disable_functions = glob, filter_list, chown, chmod, get_current_user, php_uname, putenv, getmyuid, getmypid, dl, ini_alter, ini_restore, exec, passthru, system, popen, pclose, leak, mysql_list_dbs, listen, chgrp, disk_total_space, disk_free_space, rmdir, tmpfile, link, shell_exec, proc_open, chroot, openlog, closelog, syslog, flock, socket_create_listen, socket_accept, socket_listen, sleep, umask, set_include_path, restore_include_path, symlink, imagerotate

保存文件并 现在再次启动 wamp 服务器 然后再试一次。