Jump to content

Help with SSH2 functions

- - - - -

  • Please log in to reply
5 replies to this topic

#1
shackrock

shackrock

    Learning Programmer

  • Members
  • PipPipPip
  • 66 posts
I'm trying to DL a file from one server to another.
$ftpHandle = ssh2_connect('ftp.remoteServer.net', 22)
ssh2_auth_password($ftpHandle, $userName, $password)
After a successful connection and login, I run this:
$dir = "/dl";
$handledir = opendir($dir);
But of course, this fails...and I don't know why. It says the folder doesn't exist. /dl is the absolute path on the remote server.
I have a feeling that "opendir" is looking on my local server (where this is being run), and not the remote one.

The goal here is to look in this folder and DL every file in the folder. After it downloads, it can delete it off of the remote server.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hello Scott,

This function should provide you with the appropriate resource handle for opendir to read:
PHP: ssh2_sftp - Manual

You will simply need to run opendir() on the stream wrapper that PHP provides
$sftphandle = ssh2_sftp($sshhandle);
opendir("ssh2.sftp://$sftphandle/dl");

The reason why we need to use sftp functions, is that SSH only provides a shell, when SFTP will provide the necessary environment to work with the files within much like FTP protocols.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
shackrock

shackrock

    Learning Programmer

  • Members
  • PipPipPip
  • 66 posts
Works perfect, you're a genius.

How about deleting the files now... (after I've processed them)? I can't find a similar function for that.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

shackrock said:

How about deleting the files now... (after I've processed them)? I can't find a similar function for that.

You could always use the function ssh2_sftp_unlink(), there are a few functions it provides for ease of use:
PHP: ssh2_sftp_unlink - Manual

ssh2_sftp_unlink($sftphandle, "/dl/somefile");

You should be able to use the standard unlink() function as well on the ssh.sftp:// wrapper, some more information on available wrappers and how they work is found here:
PHP: Supported Protocols and Wrappers - Manual
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
shackrock

shackrock

    Learning Programmer

  • Members
  • PipPipPip
  • 66 posts
Aha, I see!

Ok so... now this is giving me trouble:

ssh2_scp_recv($ftpHandle, '/dl/' . $fileName, $ABS_LOCAL_PATH . '/' . $fileName)

Any ideas?

#6
shackrock

shackrock

    Learning Programmer

  • Members
  • PipPipPip
  • 66 posts
Ah, I used COPY... worked like a charm with the wrapper.

THanks!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users