是否可以从一台服务器(sftp)下载文件并使用php将其上传到我的服务器? [英] Is it possible to download a file from one server (sftp) and upload it to my server using php?

查看:41
本文介绍了是否可以从一台服务器(sftp)下载文件并使用php将其上传到我的服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知这无法完成,但我想在这里获得一些其他意见.当涉及到这样的事情时,我有点新手.

我的网站:ExampleSiteA.com

要下载的文件:ExampleSiteB.com

基本上,我正在从 ExampleSiteB.com 下载一个 csv 文件以更新我的网站 ExampleSiteA.com.为此,我通过 CoreFTP 手动下载 csv 文件,然后将其手动上传到 ExampleSiteA.com.文件每天都在变化,我想跳过这一步,这样我就可以自动化这个过程.

请记住,我需要通过 SFTP 从 ExampleSiteB.com 下载 csv 文件...

如果是 SFTP,我不确定是否可以将文件从一台服务器直接下载/上传到另一台服务器.文件大小也相当大,平均约为 25,000 KB/25 MB.

我还没有探索的另一个选项是要求或包含来自另一台服务器的文件......这是一个选项还是一种可能性?该文件位于我网站专用的文件夹中,SFTP 下载需要登录.

任何见解将不胜感激.提前致谢!

解决方案

去这里下载你需要的:http://phpseclib.sourceforge.net/

更新

  • 对于 SFTP

然后在您的脚本中:

login('username', 'password')) {exit('登录失败');}回声 $sftp->pwd() ."\r\n";$sftp->put('remote.file.csv', 'yourCSV.csv', NET_SFTP_LOCAL_FILE);print_r($sftp->nlist());?>

如果您需要连接到第二台服务器进行下载:

$sftp2 = new Net_SFTP('www.serverFromWhichToDownload.com');if (!$sftp2->login('username', 'password')) {exit('登录失败');}回声 $sftp2->pwd() ."\r\n";$sftp2->get('localFileName.csv', 'remoteFileName.csv');print_r($sftp2->nlist());

阅读文档以获得更多帮助和示例:http://phpseclib.sourceforge.net/documentation/net.html#net_sftp_get

如果连接失败等,要记录连接正在执行的操作,请使用:

include('Net/SSH2.php');定义('NET_SSH2_LOGGING',真);$ssh = new Net_SSH2('www.domain.tld');$ssh->login('用户名','密码');echo $ssh->getLog();

  • 对于 FTP 上传 - SO 已经疯了,不想格式化我的代码,但无论如何:
<前>$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);如果 (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {echo "成功上传 $file\n";} 别的 {echo "上传 $file 时出现问题\n";}ftp_close($conn_id);

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!

解决方案

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);

这篇关于是否可以从一台服务器(sftp)下载文件并使用php将其上传到我的服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆