2015-04-04 127 views
1

我一直在试图让这个shell_exec()命令在我的PHP脚本中工作,但它不会不会返回任何内容。我已经从命令行成功运行它并获得预期的结果。PHP shell_exec不能使用grep和管道

有人可以帮忙调试吗?

$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/>"; 

很多谢谢!

+0

使用'getcwd()'检查当前工作目录。我运行你的代码,它运行良好。 – 2015-04-04 12:22:56

+0

感谢Bart,现在可以工作。 :) – 2015-04-04 12:36:02

回答

0

通过

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

感谢赛勒斯,完美的作品。 – 2015-04-04 12:48:51

1

exec()更换

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

已被禁用出于安全原因,您的服务器上时,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服务器 并重试。