Question

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);
?>
Was it helpful?

Solution 3

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

OTHER TIPS

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.

Try

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

in linux mod:

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

in windows mod:

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

PHP has its own change directory command: http://us3.php.net/manual/en/function.chdir.php. However that might not do exactly what you want.

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.

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

<?php
$result = system("cd \;".$command[0], $return);?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top