I'm writing a script to automate deployment from my windows development PC to a shared hosting server.
I am getting different results depending on whether I execute the commands via Putty or PHP (both running on my PC).
In putty, when I log in to the server via SSH, I can run commands like:
cd /www/
ls -la #outputs contents of /www
But when I do it via PHP with phpseclib, as below, any cd
commands are totally ignored:
<?php
require_once __DIR__.'/vendor/autoload.php';
use phpseclib\Net\SSH2;
$ssh = new SSH2('ssh.mydomain.com');
if (!$ssh->login('mydomain.com', 'mypassword')) {
trigger_error("Login Failed", E_ERROR);
}
echo $ssh->exec('pwd');
$ssh->exec('cd /www/');
echo $ssh->exec('pwd'); // unchanged
echo $ssh->exec('ls -la'); // does NOT output contents of /www/
echo $ssh->exec('ls /www/ -la'); // DOES output contents of /www/
Specifying an absolute URL in the above is an acceptable workaround. However, the following is a major problem.
If I upload a file, stuff.zip
into /www/ and then try to extract it, the following works via Putty:
unzip /www/stuff.zip -d /www/
But if I try this via PHP:
echo $ssh->exec('unzip /www/stuff.zip -d /www/');
I get the error:
unzip: cannot find or open /www/stuff.zip, /www/stuff.zip.zip or /www/stuff.zip.ZIP.
I have tried chmod 777 /www/stuff.zip
but it makes no difference.
How can I diagnose/fix this problem?