Ryan
Ryan

Reputation: 667

Can't run "cd" command in PHP shell_exec()

I recently got my laptop with Apache setup on my university's Ethernet connection. Now I can connect to my computer from anywhere as long as I have either the IP address or host name (which I can choose). Now I want to create a Web-based command prompt that will let me run commands on my laptop from any device.

One problem is that I can't run the "cd" command. I have my PHP script setup so it can run a series of commands delimited by a newline character. So I run "cd ../" and then "pwd" but it's still in the root directory of my Web app. How do I fix this?

Upvotes: 3

Views: 16781

Answers (2)

mario
mario

Reputation: 145482

If you do this:

 shell_exec("cd ..");
 shell_exec("pwd");

Then the second command will be executed with a new shell, which has the same starting directory as the first had, because it's a subprocess of the current PHP.

The changing of the current directory with the first shell exec won't last to the second one. Such a series of depended shell commands only works by executing all at once:

 shell_exec("cd .. ; pwd");

Upvotes: 13

Markus Hedlund
Markus Hedlund

Reputation: 24254

I think you have to change the directory of the current process/script. You do this with chdir. Then you can run shell_exec.

I assume you realize the severe security concerns your solution creates...

Upvotes: 12

Related Questions