-2

I have copied and tried many PHP scripts from SO posts. I am trying to download files from a server running Centos. Via psftp (putty) I can login manually and copy files. But I want to automate the process, hence the need for a script.

On a similar server running on Windows am able to download files by ftp via a simple Perl script. On the Centos server I get connection refused with the Perl script. So I tried several php scripts. Are the scripts below (from SO posts) for the job? or what is wrong with the scripts?

script 1

#!/usr/bin/php
<?php
include('Net/SSH2.php');
$sftp = new Net_SFTP('xx.xx.xxx.xxx');
if (!$sftp->login('myuser', 'mypasswd')) {
    exit('Login Failed');
}

// outputs the contents of filename.remote to the screen
echo $sftp->get('gateway_data*');
?>

Script 2

#!/usr/bin/php
<?php
include('Net/SSH2.php');

username='myuser';
password='mypasswd';
// Create SCP connection using a username and password
$scp = new SCP(
    'xx.xx.xxx.xxx',
    new SSH2Password($username, $password)
);

#################################
$sftp = ssh2_sftp($conn);

// Create a new local folder
ssh2_sftp_mkdir($sftp, './data');

// Retrieve a list of files
$files = scandir('ssh2.sftp://' . $sftp . '/data/gateway_data*');
 ################################################################
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Zilore Mumba
  • 1,346
  • 4
  • 23
  • 33
  • Please split your two questions into two separate posts. Regarding the download question, please elaborate what specific problem are you having with the code you have posted. – Martin Prikryl Dec 11 '14 at 09:40
  • The script above does not do anything, no error, except printing the result of cd data; ls; pwd; with print "$stdout";. When the print is commented, nothing happens. – Zilore Mumba Dec 11 '14 at 11:47
  • See my comment at http://stackoverflow.com/q/27346971/569976 – neubert Dec 11 '14 at 12:33
  • Hi there. On this old question I have trimmed out an unrelated secondary question, for which answers below do not seem to have been supplied. In general we try to discourage adding several questions per post - add one per post, so they can be answered (or closed) separately. – halfer Nov 13 '15 at 18:24
  • (On an unrelated note I am downvoting, as you received two responses but did not reply or vote). – halfer Nov 13 '15 at 18:24

2 Answers2

0

In the first of PHP script you have posted you're doing echo $sftp->get('gateway_data*'); whereas in the Perl script you're doing cp gateway_data_301.txt. Try doing that in the PHP script. eg. echo $sftp->get('gateway_data_301.txt');.

As is it is unclear what you're expecting to happen. Unless the file name /actually/ has a wild card in it then are you expecting it to download every file that starts off with gateway_data* and just concatenate them in the output? Personally, I think just returning false or NULL would be better than that.

neubert
  • 15,947
  • 24
  • 120
  • 212
-1

You can use your script 2 in PHP. However something is missing there. You are only opening the source directory. You must write a loop over all files in that folder.

  // Retrieve a list of files
  $files = scandir('ssh2.sftp://' . $sftp . '/data/gateway_data*');
  foreach ($files as $key => $value) {

See the example of how to send a file with SFTP using SFTPConnection.

Franz Holzinger
  • 913
  • 10
  • 20