Fluidbyte
Fluidbyte

Reputation: 5210

Can't 'cd' with PHP shell_exec()

I'm trying to write a script to browse and return similar to working in the terminal.

Most commands work fine, however cd /path/to/files just doesn't do anything.

Upvotes: 5

Views: 7724

Answers (3)

nneonneo
nneonneo

Reputation: 179422

Each command to shell_exec runs in its own shell. Thus, if you do a cd, that will only affect that command.

If you want to change directory, use chdir.

Upvotes: 8

Linda
Linda

Reputation: 143

What about PHP chdir?

http://php.net/manual/en/function.chdir.php

Or use backticks?

`cd /path/to/files`

Upvotes: 1

cwallenpoole
cwallenpoole

Reputation: 82028

You're looking for chdir. It is a PHP function.

shel_exec('cd /some/where'); actually works just fine (it changes the current shell_exec to a different directory), but it will not hold on to the current directory after the shell_exec finishes. This means that the next command won't share the same state that would have been altered by the call to cd.

Upvotes: 2

Related Questions