Reputation: 19212
In a PHP cli script I'm writing I need to change the path for the user that's running the script. The destination path is not fixed.
The script will be used on machines running on Ubuntu. With a bash script I could use sourcing, but that doesn't seem to work with PHP scripts. When I try that, the hashbang referring to the php binary at the top of the script is ignored and the script interpreted as a bash script. I don't really know what else to try.
Is there some way to achieve this?
Upvotes: 0
Views: 1132
Reputation: 1030
As I understand the problem, you have a parent process that will launch PHP in a child process to execute a PHP script. And you want the logic of that PHP script to cause the parent process' working directory to change. The PHP script cannot directly modify the parent's working directory. But ... you can have the child PHP script create a new file that contains a cd newpath
command, then source
that newly created file. So
Some main.sh
script file contains something like:
/bin/php /path/to/the/php/script.php
source /tmp/generatedscript
rm /tmp/generatedscript
script.php
should create a new tmp file that contains only a cd
command with the desired directory as target. main.sh
can source
that file, and you are now in a new wd.
Be sure to source
the main.sh
so that the commands all run within the current process.
Upvotes: 1