Search code examples
phpsshrequire

Include in remote script fails when running it via SSH


I run a script locally, which when finished makes an exec call to a script on a remote server.

exec("ssh user@server.com \"php /full/path/to/script.php\"", $output, $return);

It presents me with this error:

PHP Warning:  require(../resources/vendor/autoload.php): failed to open stream: No such file or directory in /full/path/to/script.php on line 3

I have tried charging the required script to it's full path with no success. Any ideas?


Solution

  • Because PHP sets CWD (current working directory) to where you are after SSH login (presumably the user home folder) and references to includes in the PHP file are interpreted as being relative to CWD. You should change directory to the root of your project:

    exec("ssh user@server.com \"cd /full/path/to/; php script.php\"", $output, $return);