Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't 'cd' with PHP shell_exec()

Tags:

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.

like image 618
Fluidbyte Avatar asked Sep 20 '12 21:09

Fluidbyte


Video Answer


3 Answers

What about PHP chdir?

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

Or use backticks?

`cd /path/to/files`
like image 23
Linda Avatar answered Oct 20 '22 16:10

Linda


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.

like image 108
nneonneo Avatar answered Oct 20 '22 15:10

nneonneo


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.

like image 36
cwallenpoole Avatar answered Oct 20 '22 15:10

cwallenpoole