PHP 系统(“cd \”);不工作
我曾经通过 php 文件使用系统命令 ..cd \ 正在工作,然后在几次尝试后突然停止了:|不管我做什么
<?php
$command = array("cd \","dir");
$result = system($command[0], $return);
?>
i used to use system commands through a php file ..cd \ was working then suddenly after a few tries it stopped :| no matter what i do
<?php
$command = array("cd \","dir");
$result = system($command[0], $return);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Windows 支持正斜杠
/
以及反斜杠\
,因此通过仅使用正斜杠,您应该是交叉兼容的,并且从长远来看不会那么麻烦。Wiki Path_Computing
如果您有任何错误,请发布它们,因为问题可能不是目录削减,因为你说它以前工作过。
Windows support forward slashes
/
as well as backslashes\
so by using just forward slashes you should be cross compatible and less aggravation in the long run.Wiki Path_Computing
If you have any errors then please post them as the issue may not be the directory slashing because you said it was previously working.
尝试
Try
我想我找到了问题所在..
PHP 中的 system() 命令不会创建 cmd.exe 的实例
相反,它只执行命令并退出。
这就是为什么当您使用 CD 命令更改目录时
..由system()创建的实例的目录被更改,然后退出。我希望我的理论是正确的xD
I think i figured out the problem..
the system() command in PHP does not create an instance of a cmd.exe
instead it only executes the command and exits.
that is why when you change directory using CD command
..the directory of the instance created by system() is changed and then exits.i hope my theory is correct xD
在 Linux 模式中:
在 Windows 模式中:
in linux mod:
in windows mod:
PHP 有自己的更改目录命令: https://www.php.net/手册/en/function.chdir.php。然而,这可能并不完全符合您的要求。
PHP has its own change directory command: https://www.php.net/manual/en/function.chdir.php. However that might not do exactly what you want.
在我的测试中(在 Win 7 和 XP 上),我发现有必要对目录转义
\
(即使用\\
代替)。或者您可以使用正斜杠。另外,为什么不使用 PHP 的chdir
函数 来代替系统调用?编辑响应评论“...我正在尝试在浏览器中创建一个cmd。...”: 系统函数 只是运行您指定的命令并返回。它不会(直接)影响调用系统命令的进程。因此,如果您正在尝试执行更改目录的命令,则系统调用不会影响正在运行的 PHP 程序的工作目录。
In my testing (on Win 7 and XP), I found it necessary to escape the
\
for the directory (i.e. use\\
instead). Or you can use the forward slash. Also, why not use PHP'schdir
function instead of the system call?Edit in response to the comment "... i am trying to create a cmd in a browser thing. ...": The system function just runs the command you specify and returns. It will not (directly) affect the process which invoked the system command. Thus, a system call with a command to change directory will not affect running PHP program's working directory, if that's what you are attempting.
为什么不能在同一个 system() 调用中使用分隔符来使用两个命令?
Why can't you user two commands in the same calling of system(), using the delimiter?