简体   繁体   English

php exec()没有执行命令

[英]php exec() is not executing the command

I have tried to use exec() with 'whoami' to check if it works and I got the result of 我试图使用exec()'whoami'检查它是否有效,我得到了结果

nt authority\system

Now I need to run a .exe file with parameters from php via exec() function. 现在我需要通过exec()函数运行带有php参数的.exe文件。

I tried this in command prompt and it actually runs the program with given parameters. 我在命令提示符下尝试了这个,它实际上运行带有给定参数的程序。 This is the example command. 这是示例命令。


NOTE the exe file gets 3 inputs (folder, file_name, report_file_nmae) 注意 exe文件获得3个输入(文件夹,file_name,report_file_nmae)

> ..\..\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. 这是我第一次使用exec()函数,所以我不熟悉它的细节。 What is wrong? 怎么了?

I tried using: 我试过用:

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

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'); 甚至exec('notepad.exe'); is not working. 不管用。 Something has to be done with php configurations maybe? 有些东西必须用php配置吗?

I already said that I was new to exec() function. 我已经说过我是exec()函数的新手。 After doing some more digging, I came upon 2>&1 which needs to be added at the end of command in exec() . 在做了一些挖掘之后,我遇到了2>&1 ,需要在exec()的命令末尾添加。

Thanks @mattosmat for pointing it out in the comments too. 感谢@mattosmat在评论中指出它。 I did not try this at once because you said it is a Linux command, I am on Windows. 我没有立刻尝试这个,因为你说它是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 ;) 感谢所有帮助人员,我很感激;)

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时解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM