Using the SSH2 Function in PHP
Articles
If you have a project that requires remote server access and file transfer, it’s likely that you have to upload files to the server regularly. For security reasons, it is not a good idea to keep the files on your local system, so you need to use a secure protocol like SFTP or SCP to transfer them.
PHP supports a set of SSH2 functions to enable you to perform SFTP and SCP tasks. This functionality can either be accessed via handy classes that simplify the code you need to execute, or directly from the ssh2_scp_* function.
I’ve had to write some code to work with ssh2_scp_send(), because it often fails if you try to copy the local path from an ssh2_scp_recv() call. The problem is that ssh2_scp_send() expects paths rooted at the base of the file system, not the wrapper path. The good news is that a little bit of research has revealed the solution – pass the wrapped path to pathinfo() first. This will give you a full, canonical path which will be a good match for the expected remote path.