php exec()未执行命令 [英] php exec() is not executing the command

查看:220
本文介绍了php exec()未执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将exec()'whoami'一起使用以检查其是否有效,并且得到了

I have tried to use exec() with 'whoami' to check if it works and I got the result of

nt authority\system

现在,我需要通过exec()函数使用php中的参数运行.exe文件.

Now I need to run a .exe file with parameters from php via exec() function.

我在命令提示符中尝试了此操作,它实际上以给定的参数运行程序.这是示例命令.

I tried this in command prompt and it actually runs the program with given parameters. This is the example command.


注意.exe文件获得3个输入(文件夹,file_name,report_file_nmae)


NOTE the exe file gets 3 inputs (folder, file_name, report_file_nmae)

> ..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml

但是当我从php文件运行此命令时:

But when I run this command from php file:

exec('..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml');

什么都没有发生.这是我第一次使用exec()函数,因此我对其细节不熟悉. 怎么了?

nothing is happening. This is the first time I am using exec() function, so I am not familiar with its details. What is wrong?

我尝试使用:

  • \\而不是\
  • escapeshellarg()在目录上
  • 在目录文件夹名称周围添加了""
  • \\ instead of \
  • escapeshellarg() on the directory
  • added "" around directory folder names

没有运气

附录:

echo exec($command)  // echos < .... why?

exec($command, $output);
print_r($output);        // Array()

我什至将文件的权限更改为对所有用户完全控制. 如果从command prompt调用程序,则可以看到时钟旁边的图标显示一秒钟.

I even changed the permission on the file to full control to all users. If I call the program from command prompt, I can see the icon appearing next to clock for a second.

但是从php进行的相同调用甚至不会调用该程序.

But the same call from php will not even call the program.

修改

即使exec('notepad.exe');无法正常工作.可能需要对php配置做些什么?

Even exec('notepad.exe'); is not working. Something has to be done with php configurations maybe?

推荐答案

我已经说过我是exec()函数的新手.经过更多的挖掘之后,我遇到了2>&1,需要在exec()的命令末尾添加它.

I already said that I was new to exec() function. After doing some more digging, I came upon 2>&1 which needs to be added at the end of command in exec().

也感谢@mattosmat在评论中指出.我没有立即尝试此操作,因为您说这是Linux命令,我在Windows上.

Thanks @mattosmat for pointing it out in the comments too. I did not try this at once because you said it is a Linux command, I am on Windows.

所以,我发现,该命令实际上是在后端执行的.这就是为什么我看不到它实际上正在运行的原因,而这正是我所期望的.

So, what I have discovered, the command is actually executing in the back-end. That is why I could not see it actually running, which I was expecting to happen.

对于所有遇到类似问题的人,我的建议是使用该命令. 它将指出所有错误,并告诉您有关执行的信息/详细信息.

For all of you, who had similar problem, my advise is to use that command. It will point out all the errors and also tell you info/details about execution.

exec('some_command 2>&1', $output);
print_r($output);  // to see the response to your command

感谢所有帮助人员,我很感激;)

Thanks for all the help guys, I appreciate it ;)

这篇关于php exec()未执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆