php exec:不返回输出
我有这个问题: 在 ISS Web 服务器上,安装了 windows 7 x64 professional、zend 服务器。在 php 下运行此命令:
exec('dir',$output, $err);
$output isempty, $err=1.
所以 exec 没有返回输出,似乎有一些错误。 Php disable_functions
为空,php 未处于安全模式,是标准的,我检查了所有选项。 这似乎是一个普遍的错误,即使在谷歌上搜索也没有给出结果。
请写下每个人的经验和最终的解决方案或解决方法。
I have this problem:
On a ISS web server, windows 7 x64 professional, zend server installed. Running this command under php:
exec('dir',$output, $err);
$output is empty, $err=1.
So exec is not returing the output, it seems has some errors.
Php disable_functions
is empty, php is not on safe mode, is standard, I check all option.
It seems to be a general errors, even search on google do not give results.
Please write every one his experince and eventual solutions or workarounds.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
PHP 手册的相关部分有一些帖子,例如这个:
还有一些关于
exec
和shell_exec
手册页。也许按照它们进行操作会让它启动并为你工作。There are a few posts to the relevant sections of the PHP Manual such as this one:
There are a few more on both the
exec
andshell_exec
manual pages. Perhaps following through them will get it up and working for you.从诸如
dir
之类的命令中获得零输出的主要原因是dir
不存在。它是命令提示符的一部分,这个特定问题的解决方案很好,就在这个页面的某个地方。您可以通过按
WIN + R
,然后输入dir
或start
来找到这一点 - 会出现一条错误消息!不管这听起来有多么反常,我发现在 Windows 中执行任何与进程相关的操作的最可靠方法是使用组件对象模型。你说让我们分享我们的经验,对吗?
$me 听到人们在笑
恢复镇静了吗?
因此,首先我们将创建 COM 对象:
然后,我们将只运行需要运行的内容。
凉爽的!现在,它将挂起,直到该二进制文件中的所有内容都完成为止。所以,我们现在需要做的就是获取输出。
您还可以做一些其他事情 - 找出进程 ID、错误代码等。尽管这个示例< /a> 适用于 Visual Basic,它基于相同的方案。
The main reason you get zero output from a command such as
dir
is becausedir
doesn't exist. It's part of the command prompt, and the solution to this particular problem is well, in this page somewhere.You can find this out by pressing
WIN + R
, and typing indir
orstart
for that matter - an error message appears!However perverse this may sound, I've found that the most reliable way to do anything process related in Windows is with using the Component Object Model. You said for us to share our experiences, right?
$me hears people laughing
Regained your composure yet?
So, first we'll create the COM object:
Then, we'll just run what ever needs to be run.
Cool! Now, that'll hang until everything's finished in that binary. So, what we need to do now is grab the output.
There's some other things you can do - find out what was the process ID, error code, etc. Although this example is for Visual Basic, it's based on the same scheme.
编辑:经过一些研究,我看到执行 DIR 命令的唯一方法是这样的:
所以你可以尝试使用我的解决方案:
你也可以使用 proc_open 函数,它使你可以访问 stderr、返回状态代码和 stdout。
与 popen 或 exec 等其他函数相比,proc_open 的有趣之处在于它提供了特定于 Windows 平台的选项,例如:
EDIT: After a few researchs, the only way i see to execute the DIR command is like that:
So you can try my solution using:
You can use proc_open function too, which gives you access to stderr, return status code and stdout.
The interesting part with proc_open, compared to other functions like popen or exec, is that it provides options specific to windows platform like:
试试这个:
它在启用了 UAC 的 Windows 8.1 64 位上运行良好。
Try this:
It works fine on Windows 8.1 64bits with UAC enabled.
我知道我已经选择了一个答案,因为我必须这样做,但我仍然不明白为什么它不能在我的机器上工作,但我使用另一种方法找到了一个后备(使用 proc_open)解决方案:
希望这对某人有帮助。
I know I have choosen an answer as I must do it but I still do not understand why it wont work on my macchine but I found a fallback (using proc_open) solution using another method:
Hope this helps someone.
我遇到了同样的问题,在花了很多时间和一杯咖啡之后,
在 Windows 7 中禁用用户帐户控制 (UAC)
短路径:P
C:\Windows\System32\UserAccountControlSettings.exe
并选择“从不通知”
并且 php exec() 工作正常!
我用:
Apache 版本:2.2.11
PHP 版本:5.2.8
Windows 7 SP1 32 位
致以诚挚的问候! :D
I was the same problem and after spend a lot of hours and cup of coffee
Just Disabling User Account Control (UAC) in Windows 7
short path :P
C:\Windows\System32\UserAccountControlSettings.exe
and select "Never notify"
and php exec() works fine!
I use:
Apache Version : 2.2.11
PHP Version : 5.2.8
Windows 7 SP1 32bit
Best regards! :D
如果您在 Windows 计算机上并尝试运行如下所示的可执行文件:
并且没有输出或您的文件未启动,那么您应该尝试以下操作:
它应该可以工作。
如果使用脚本当前所在文件夹的相对路径,这尤其方便,例如:
如果需要运行其他程序,请不要忘记 chdir() 返回原始文件夹。
If you are on Windows machine and trying to run an executable like this:
and there is no output or your file isn't started, then you should try this:
It should work.
This is particularly handy if one is using relative path from folder where script currently is being located, for example:
And don't forget to chdir() back to originating folder if you need to run other programs.
出于安全目的,您的服务器可能限制对“exec”命令的访问。在这种情况下,也许您应该联系托管公司以删除此限制(如果有)。
For security purposes, your server may have restricted access to "exec" command. In this case, maybe you should contact the hosting company to remove this restriction (if there is one).
您可以检查 IIS 用户对 cmd.exe 的权限
如果输出中出现类似 (COMPUTERNAME=您的计算机名称) 的行:
则表示该用户没有执行 cmd 的权限。
您可以使用命令添加执行权限:
You can check permisssions of IIS User to cmd.exe
If in output is line like (COMPUTERNAME=your computer name):
It means that user has no rights to execute cmd.
You can add execute permissions with command:
看看Apache error_log,也许你会发现错误信息。
另外,您可以尝试使用 popen 而不是 exec。它提供了更多控制,因为您可以将进程启动与输出读取分开:
Take a look at Apache error_log, maybe you'll find the error message.
Also, you can try using popen instead of exec. It gives more control because you can separate process start from output read: