php exec() 没有执行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17914402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php exec() is not executing the command
提问by Brian
I have tried to use exec()
with 'whoami'
to check if it works and I got the result of
我试图用exec()
with'whoami'
检查它是否有效,我得到了结果
nt authority\system
Now I need to run a .exe
file with parameters from php
via exec()
function.
现在我需要运行一个.exe
带有来自php
viaexec()
函数的参数的文件。
I tried this in command promptand it actually runs the program with given parameters. This is the example command.
我在命令提示符下试过这个,它实际上用给定的参数运行程序。这是示例命令。
NOTEthe exe file gets 3 inputs (folder, file_name, report_file_nmae)
注意exe 文件有 3 个输入(文件夹、文件名、报告文件名)
> ..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml
But when I run this command from php
file:
但是当我从php
文件运行这个命令时:
exec('..\..\some_file.exe folder="C:\path_to_folder" param=1.xml report=2.xml');
nothing is happening. This is the first time I am using exec() function, so I am not familiar with its details. What is wrong?
什么都没有发生。这是我第一次使用 exec() 函数,所以我不熟悉它的细节。怎么了?
I tried using:
我尝试使用:
\\
instead of\
escapeshellarg()
on the directory- added
""
around directory folder names
\\
代替\
escapeshellarg()
在目录上""
在目录文件夹名称周围添加
No luck
没运气
Addendum:
附录:
echo exec($command) // echos < .... why?
or
或者
exec($command, $output);
print_r($output); // Array()
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.
我什至将文件的权限更改为所有用户的完全控制权。如果我从 调用程序command prompt
,我可以看到时钟旁边出现图标一秒钟。
But the same call from php
will not even call the program.
但是来自相同的调用php
甚至不会调用该程序。
Edit
编辑
Even exec('notepad.exe');
is not working. Something has to be done with php
configurations maybe?
甚至exec('notepad.exe');
不工作。php
也许必须对配置做些什么?
回答by Brian
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()
.
我已经说过我是新来的exec()
。在做了更多的挖掘之后,我发现2>&1
需要在exec()
.
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.
也感谢您@mattosmat
在评论中指出。我没有立即尝试这个,因为你说这是一个 Linux 命令,我在 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 ;)
感谢所有帮助我的人,我很感激;)
回答by craned
You might also try giving the full path to the binary you're trying to run. That solved my problem when trying to use ImageMagick
.
您也可以尝试提供您尝试运行的二进制文件的完整路径。这解决了我尝试使用ImageMagick
.