PHP Exec() Command: How to Specify Working Directory

PHP exec() command: how to specify working directory?

Either you change to that directory within the exec command (exec("cd Scripts && ./script.sh")) or you change the working directory of the PHP process using chdir().

Changing directory in shell_exec does not work

Solution1

Use full path to file

$message = shell_exec($work_dir . "/" . $work_file . " 2>&1");

Solution2

Also you can use chdir() for this. It will change current working directory of the php script.

chdir($work_dir);
$message = shell_exec($work_file . " 2>&1");

php shell_exec() change user or navigate to user folder

Yes you can. You can change directory with cd command inside shell_exec(). But make sure you write subsequent commands in the same line after putting ;

Like this,

shell_exec("cd .. ; pwd");

Above function will move directory to current's parent directory and display present working directory.

Can't 'cd' with PHP shell_exec()

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.

php exec in which directory I am?

You can either use the pwd command or PHP's built-in function getcwd() to get the working directory. The latter is recommended.

However, if you know the absolute path of your PHP script, you should just specify that and don't care about what directory you're currently in.

exec("php /path/to/somescript.php");

Run command in other folder

Try changing the working directory first

e.g.

chdir('/path/to/1.6.2');
exec("sencha app build production");


Related Topics



Leave a reply



Submit