1

I have been told this cannot be done but I want to get some other opinions here. I am a bit of a newbie when it comes to things like this.

My Site: ExampleSiteA.com

File to download: ExampleSiteB.com

Basically, I am downloading a csv file from ExampleSiteB.com to make updates to my site, ExampleSiteA.com. To do this, I am downloading the csv file manually through CoreFTP and then uploading it manually to ExampleSiteA.com. The file changes daily and I would like to skip this step so I can automate the process.

Keep in mind that I need to download the csv file from ExampleSiteB.com through SFTP...

I am not sure if it is possible to directly download/upload a file from one server to another if one is SFTP. The file size is also quite large, it averages about 25,000 KB / 25 MB.

Another option that I haven't explored yet is requiring or including a file from another server... is that an option or a possibility? The file is located in a folder exclusively for my site and a login is required for SFTP download.

Any insight will be appreciated. Thanks in advance!

NotJay
  • 3,919
  • 5
  • 38
  • 62

5 Answers5

7

Go here and download what you need: http://phpseclib.sourceforge.net/

UPDATE

  • FOR SFTP

Then in your script:

<?php
include('Net/SFTP.php');

$url = 'http://www.downloadsite.com';
$fileToDownload = "yourCSV.csv";
$cmd = "wget -q \"$url\" -O $fileToDownload";
exec($cmd);

$sftp = new Net_SFTP('www.uploadsite.com');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

echo $sftp->pwd() . "\r\n";
$sftp->put('remote.file.csv', 'yourCSV.csv', NET_SFTP_LOCAL_FILE);
print_r($sftp->nlist());
?>

If you need to connect to a second server for download:

$sftp2 = new Net_SFTP('www.serverFromWhichToDownload.com');
if (!$sftp2->login('username', 'password')) {
    exit('Login Failed');
}

echo $sftp2->pwd() . "\r\n";
$sftp2->get('localFileName.csv', 'remoteFileName.csv');
print_r($sftp2->nlist());

Read the docs for further help and examples: http://phpseclib.sourceforge.net/documentation/net.html#net_sftp_get

To Log what your connection is doing if it fails, etc. use this:

include('Net/SSH2.php');
define('NET_SSH2_LOGGING', true);
$ssh = new Net_SSH2('www.domain.tld');
$ssh->login('username','password');
echo $ssh->getLog();
  • FOR FTP upload - SO has gone crazy, does not want to format my code, but here it is anyway:
$file = 'somefile.txt';
$remote_file = 'readme.txt';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
    echo "successfully uploaded $file\n";
} else {
    echo "There was a problem while uploading $file\n";
}
ftp_close($conn_id);
Delimitry
  • 2,987
  • 4
  • 30
  • 39
reuf
  • 550
  • 4
  • 12
  • Login Failed... Expected SSH_FXP_VERSION in ../../../Net/SFTP.php on line 376 – NotJay Jun 11 '12 at 16:02
  • 1. Try downloading the latest version of SSH2.php and SFTP.php and let me know if they fix the problem. 2. Check if your passwords and logins work. – reuf Jun 11 '12 at 16:05
  • Is this code to download from sftp and upload to normal ftp or vice versa? – NotJay Jun 11 '12 at 16:08
  • Both of the codes are for sftp, while the first code is to download file from publicly available HTTP resource...I updated the first post and included the code to log what is happening when you connect, etc... – reuf Jun 11 '12 at 16:10
  • I am not downloading from a publicly available http... the original file is located on an sftp... then i need to upload it to regular ftp (my server). I'm a newbie at this, sorry for any confusion. – NotJay Jun 11 '12 at 16:16
  • Confirm if it failed or if it succeeded, or if you dont see my code for uploading to regular FTP... – reuf Jun 11 '12 at 16:37
  • It works! Thank you for your help! Sorry for the delay, I had to eat something :-D – NotJay Jun 11 '12 at 16:51
  • It was working great on a test file but now it seems to not work when I am using the actual file. Could it be the file size is too large? Any settings I can change to make this work properly? – NotJay Jun 11 '12 at 17:43
  • The file appears with the correct last modified date and time but the file size then becomes 0.0 kb. What's going wrong? – NotJay Jun 11 '12 at 17:58
  • What is the size of the file you are uploading? – reuf Jun 11 '12 at 18:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12404/discussion-between-notjay-and-reuf) – NotJay Jun 11 '12 at 18:05
  • No luck. I checked my host company and the 3rd party sftp account... no blocks. Back to square one. – NotJay Jun 18 '12 at 19:29
  • Reuf, I got this working. I have pasted my code below to help others with this! Thanks for your help, I appreciate it! – NotJay Jun 18 '12 at 20:51
  • Reuf, One quick question... do you know what might be causing the csv file to have empty rows after each row in the csv? Could it be something in Source Forge code? – NotJay Jun 19 '12 at 17:15
  • One possibility is that you append end of line character (CR, LF) before you append the csv data line. Try opening your csv in notepad++ and then chose from menu: View > Show Symbol > Show End of Line...Check the code that generates csv and see how each line gets appended and if \n gets appended before the csv string...Send an email (or add me to contacts) to muhamed.halil [at] gmail if you want to meet up on chat to discuss this and similar issues futher... – reuf Jun 19 '12 at 22:24
2

Yes, that's possible using ssh2_sftp.

http://php.net/manual/en/function.ssh2-sftp.php

Sherlock
  • 7,525
  • 6
  • 38
  • 79
  • 1
    Then follow that up with `file_put_contents` to save the stream to a file on the destination server. http://php.net/manual/en/function.file-put-contents.php – jwatts1980 Jun 11 '12 at 13:42
  • This looks good... can't wait to try it. I will have to let you know how it works! Thanks to both of you! – NotJay Jun 11 '12 at 13:45
  • :-( - Call to undefined function ssh2_connect() What am I doing wrong? – NotJay Jun 11 '12 at 14:37
  • Remove the ; before extension=php_ssh2.dll in php.ini and restart your server. – Sherlock Jun 11 '12 at 14:41
2

I have had good luck with cURL in the past. If you are on a Linux box, it would be trivial to set up a CRON job to do this update process for you. A good reference for CLI HTTP scripting in cURL can be found here, however you may need the -T flag (for file transport) to accomplish the upload portion. Speaking of uploading, if you can run the script/process/crontab from the server you would like to update, I would recommend downloading from the web server to obviate one trip and a third party. Or, if you need to update on demand, you could write a PHP script that uses the built in PHP cURL functions. If you take the Linux+CLI route, you could also use sftp.

Update: In testing curl with sftp (curl -u uname:pword sftp://domain.tld) I get the following error: curl: (1) Protocol sftp not supported or disabled in libcurl on Kubuntu 12.04. So cURL may not be a good idea. I also tested CLI sftp (sftp uname@domain.tld:/dir/file.ext) but could not find a way (short of using ssh keys) to send authentication. Thus, this would necessarily be a manual process unless you did set up ssh keys between the servers. As it does not sound like you have that kind of access to ExampleSiteB.com, this probably isn't acceptable.

Update 2: Since my initial answer turned out to be of little use, I figured I would expand upon one of the above answers. I was trying to find a solution that did not involve a PECL extension, but I did not have much luck with ftp_ssh_connect(). I recommend trying it, you may have better luck and could forgo the PECL extension route.

Sigh, on further reading, it appears ftp_ssh_connect is, understandably, incompatible with the sftp protocol. However, I found a nice blog post about utilizing ssh2_connect() and ssh2_sftp() (as mentioned in a previous answer) and figured I would post that to give you some additional assistance. It is not as simple as calling the functions for most PHP distributions. Here is the blog post. Some of those steps may not be necessary or you may need to do some additional things listed in another blog post I ran across, here.

On my system, all I had to do was run apt-get install libssh2-1-dev libssh2-php and I was able to find ssh2 in my php -m output.

Eric H
  • 1,100
  • 16
  • 32
  • I wish I could work on this now... I cannot wait to work on this. It's been a pain to do this manually every day. Thanks for your help and I will keep you posted on my progress! – NotJay Jun 11 '12 at 13:54
  • Thank you for the update and the information. I appreciate your help very much. – NotJay Jun 11 '12 at 14:08
  • Thank you again for your work... I have been trying many things myself and have not yet had success. – NotJay Jun 11 '12 at 15:34
1

Having an include, as long as you have read/write permissions on the website you're getting the file from should work, however this is just guess work atm as i don't have any means of checking it. Good luck though!

Connor
  • 11
  • 1
1

Yes, you should be able to do this.

Whoever told you that you can't do this might be getting confused with JavaScript and cross-site scripting browser restrictions which prevent JavaScript downloaded from one domain to access content in a different domain.

That being said, if you are using PHP which to me implies that you are talking about PHP running on a web sever, you should be able to use PHP or any other scripting or programming language to download the file from SiteB.com, then update the file, and then finally FTP the file to a different web server (SiteA.com).

HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41