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");
Solution2Also 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
Check If Two PHP Datetime Objects Are Set to the Same Date ( Ignoring Time )
Calling a PHP Function from an HTML Form in the Same File
Why Doesn't PHP Permit Private Const
How to Extract Frames of an Animated Gif with PHP
How to Access Elements in an Array Returned from a Function
Why Use Output Buffering in PHP
PHP Considers Null Is Equal to Zero
Why Is MySQLi_Insert_Id() Always Returning 0
How to Upload Files in Web Workers Without Using Formdata
"Call to Undefined Function" Error When Calling Class Method
PHP Require File from Top Directory
Change Mime Type of Output in PHP
How to Get the Execution Time of a MySQL Query from PHP
Visits Counter Without Database with PHP
Missing CSS File and Images After Url Rewrite
Php: Require Path Does Not Work for Cron Job