PHP 系统(“cd \”);不工作

发布于 2024-10-05 05:08:15 字数 181 浏览 0 评论 0原文

我曾经通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

何必那么矫情 2024-10-12 05:08:15

Windows 支持正斜杠 / 以及反斜杠 \,因此通过仅使用正斜杠,您应该是交叉兼容的,并且从长远来看不会那么麻烦。

<?php
    $command = array("cd /","dir");
    $result = system($command[0], $return);
?>

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.

<?php
    $command = array("cd /","dir");
    $result = system($command[0], $return);
?>

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.

故事还在继续 2024-10-12 05:08:15

尝试

$command = array("cd \\","dir"); 

Try

$command = array("cd \\","dir"); 
黑色毁心梦 2024-10-12 05:08:15

我想我找到了问题所在..
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

口干舌燥 2024-10-12 05:08:15

在 Linux 模式中:

$command = array("cd \\","dir");

在 Windows 模式中:

$command = array("cd /","dir");

in linux mod:

$command = array("cd \\","dir");

in windows mod:

$command = array("cd /","dir");
北座城市 2024-10-12 05:08:15

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.

月光色 2024-10-12 05:08:15

在我的测试中(在 Win 7 和 XP 上),我发现有必要对目录转义 \ (即使用 \\ 代替)。或者您可以使用正斜杠。另外,为什么不使用 PHP 的 chdir 函数 来代替系统调用?

<?php
    echo getcwd() . "\n";
    chdir("\\");
    echo getcwd() . "\n";
?>

编辑响应评论“...我正在尝试在浏览器中创建一个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's chdir function instead of the system call?

<?php
    echo getcwd() . "\n";
    chdir("\\");
    echo getcwd() . "\n";
?>

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.

豆芽 2024-10-12 05:08:15

为什么不能在同一个 system() 调用中使用分隔符来使用两个命令?

<?php
$result = system("cd \;".$command[0], $return);?>

Why can't you user two commands in the same calling of system(), using the delimiter?

<?php
$result = system("cd \;".$command[0], $return);?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文