在 php 中运行 windows 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17830276/
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
Run windows command in php
提问by Shawon
Is it possible to run windows command line code from php ? My windows command line code is:
是否可以从 php 运行 Windows 命令行代码?我的 Windows 命令行代码是:
<?php
error_reporting(E_ALL);
try {
echo exec('C:\xampp\mysql\bin>mysqlbinlog --start-datetime="2011-04-21 10:31:44" c:\xampp\mysql\data\binlog\bin-log.000001 > c:\xampp\mysql\data\binlog\sql.txt');
} catch (Exception $e) {
echo $e->getMessage();
}
Now I want to run this code from PHP using system()
or exec()
etc.
any help appreciated.
现在我想使用system()
或exec()
等任何帮助从 PHP 运行此代码。
回答by Loki
If you are able to run your commands manually from the command-line, but not via WAMP, than the user the Apache Service runs as does not have the needed permissions to execute your commands and binaries (nor interact with the desktop if it is GUI related).
如果您能够从命令行手动运行您的命令,但不能通过 WAMP,那么运行 Apache 服务的用户没有执行您的命令和二进制文件所需的权限(如果是 GUI,也不能与桌面交互有关的)。
By default, Apache service run under user account: nt authority\system
默认情况下,Apache 服务在用户帐户下运行:nt authority\system
Change it to your custom user (user with administrative rights) by following below steps,
按照以下步骤将其更改为您的自定义用户(具有管理权限的用户),
- Hit Windows+R which opens Run
- Type "services.msc" which opens list of windows services
- Find and select your apache service and open its properties
- Go to Logon tab and change the account From “Local System” to another user/account
- Restart your Apache service
- 点击 Windows+R 打开运行
- 键入“services.msc”,打开 Windows 服务列表
- 查找并选择您的 apache 服务并打开其属性
- 转到登录选项卡并将帐户从“本地系统”更改为另一个用户/帐户
- 重启你的 Apache 服务
Happy coding :-)
快乐编码:-)
回答by asmcod
I'm using wamp, and the only solution was the followings:
我正在使用 wamp,唯一的解决方案如下:
At Control Panel / Administrative tools / Services, search for wampapache64, httpd, or something like that. On the Log On tab tick the 'allow service to interact with desktop'
在控制面板/管理工具/服务中,搜索 wampapache64、httpd 或类似内容。在登录选项卡上勾选“允许服务与桌面交互”
Hope this helps!
希望这可以帮助!
回答by Ermir
If you can't run the command directly in exec(), then what you can do is make a batch file with the command and place it on the root of your website. Then, just run:
如果你不能直接在 exec() 中运行命令,那么你可以做的是用命令制作一个批处理文件并将它放在你网站的根目录下。然后,只需运行:
<?php echo exec("script.bat"); ?>
回答by Rik
C:\xampp\mysql\bin>mysqlbinlog
is not a command.
C:\xampp\mysql\bin>mysqlbinlog
不是命令。
I think you mean C:\xampp\mysql\bin\mysqlbinlog
.
我想你的意思是C:\xampp\mysql\bin\mysqlbinlog
。
Notice the replacing of the >
in a \
.
请注意替换>
a 中的\
。
The >
is only visible in the command prompt as a separator (for the eyes) but you should not use it like that in a command unless you are trying to redirect your output. (which you are doing again later in the line). So just replace that first >
in a \
and your command will run.
该>
提示作为分隔符(的眼睛)仅在命令可见,但除非你想你重定向输出你不应该在命令中使用它这样。(您稍后将再次执行此操作)。因此,只需>
在 a中先替换它\
,您的命令就会运行。
回答by Jeremy
Just give it a try. Or if you want a sandbox example just try to run
试试吧。或者,如果您想要一个沙箱示例,请尝试运行
<?php echo exec("whoami");?>