1

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.

Shackrock
  • 4,601
  • 10
  • 48
  • 74
  • "But of course, this fails..." - Why is it obvious that it fails. You write "of course". Why do you write so? What is the error message? – hakre Jul 12 '11 at 13:04
  • http://php.net/manual/en/function.opendir.php - Check the second parameter of opendir. You need to give context. – hakre Jul 12 '11 at 13:05
  • @hakre What is "Context"? Does it just want my stream from the SSH2 session? – Shackrock Jul 12 '11 at 13:11
  • 1
    Yes, the `$ftpHandle` I think. – hakre Jul 12 '11 at 13:17
  • That was not entirely correct but in the right direction, I've added an answer that better explains it. – hakre Jul 12 '11 at 13:43

1 Answers1

1

After re-reading your question, I think the problem is, that you don't tell opendir() that it should operate on the SSH2 connection. By default, it's the local filesystem, but you want it to operate on your SSH connection.

To operate on the SSH connection, initialize the SSH SFTP subsystem PHP Manual first and then access resources via the SSH2 SFTP filesystem wrapper:

$sftp = ssh2_sftp($ftpHandle);
$handledir = opendir("ssh2.sftp://$sftp$dir");
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 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. – Shackrock Jul 12 '11 at 23:01
  • @Schackrock: All functions that support file wrappers will do. I think it's unlink, just browse the PHP manual a bit. – hakre Jul 13 '11 at 01:31
  • Ok so... now this is giving me trouble: Code: ssh2_scp_recv($ftpHandle, '/dl/' . $fileName, $ABS_LOCAL_PATH . '/' . $fileName) Any ideas? – Shackrock Jul 13 '11 at 13:19
  • I would use sftp instead of scp. – hakre Jul 13 '11 at 13:22
  • there is no SFTP choice for DLing a file though: http://us.php.net/manual-lookup.php?pattern=ssh2_sftp_rec&lang=en – Shackrock Jul 13 '11 at 13:25
  • hmmm, I guess I don't understand how opening will suffice - I want to copy it to my local server drive... THEN do some work on it. – Shackrock Jul 13 '11 at 13:37
  • You open the file in the remote location with fopen(). Then you fetch the file's data and store it into a local file. – hakre Jul 13 '11 at 14:08
  • I tried that - but that was over my head. I ended up using "copy" which also supports wrappers. worked like a charm! Thanks again. – Shackrock Jul 13 '11 at 14:36