简体   繁体   English

是否可以从一台服务器(sftp)下载文件并将其使用php上传到我的服务器?

[英]Is it possible to download a file from one server (sftp) and upload it to my server using php?

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 我的网站:ExampleSiteA.com

File to download: ExampleSiteB.com 下载文件:ExampleSiteB.com

Basically, I am downloading a csv file from ExampleSiteB.com to make updates to my site, ExampleSiteA.com. 基本上,我是从ExampleSiteB.com下载一个csv文件来更新我的网站ExampleSiteA.com。 To do this, I am downloading the csv file manually through CoreFTP and then uploading it manually to ExampleSiteA.com. 为此,我要通过CoreFTP手动下载csv文件,然后手动将其上传到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... 请记住,我需要通过SFTP从ExampleSiteB.com下载csv文件...

I am not sure if it is possible to directly download/upload a file from one server to another if one is SFTP. 如果一台服务器是SFTP,我不确定是否可以将文件从一台服务器直接下载/上传到另一台服务器。 The file size is also quite large, it averages about 25,000 KB / 25 MB. 文件大小也很大,平均约为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. 该文件位于我的站点专用的文件夹中,并且需要登录才能下载SFTP。

Any insight will be appreciated. 任何见识将不胜感激。 Thanks in advance! 提前致谢!

Go here and download what you need: http://phpseclib.sourceforge.net/ 转到此处下载所需内容: http : //phpseclib.sourceforge.net/

UPDATE 更新

  • FOR SFTP 对于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 阅读文档以获取更多帮助和示例: 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: FOR 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);
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);

Yes, that's possible using ssh2_sftp. 是的,可以使用ssh2_sftp。

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

I have had good luck with cURL in the past. 过去,我对cURL感到很幸运。 If you are on a Linux box, it would be trivial to set up a CRON job to do this update process for you. 如果您在Linux机器上,则设置一个CRON作业来为您执行此更新过程将很简单。 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. 可以在此处找到有关cURL中CLI HTTP脚本的很好的参考,但是您可能需要-T标志(用于文件传输)才能完成上传部分。 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. 说到上传,如果您可以从要更新的服务器上运行脚本/进程/ crontab,我建议您 Web服务器上下载以免一次旅行和第三方旅行。 Or, if you need to update on demand, you could write a PHP script that uses the built in PHP cURL functions . 或者,如果您需要按需更新,则可以编写一个使用内置PHP cURL函数的PHP脚本。 If you take the Linux+CLI route, you could also use sftp . 如果采用Linux + CLI路由,则也可以使用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. 更新:在使用sftp测试curl( curl -u uname:pword sftp://domain.tld )时,出现以下错误: curl: (1) Protocol sftp not supported or disabled in libcurl Kubuntu 12.04上的curl: (1) Protocol sftp not supported or disabled in libcurl So cURL may not be a good idea. 因此,cURL可能不是一个好主意。 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. 我还测试了CLI sftp( sftp uname@domain.tld:/dir/file.ext ),但是找不到发送身份验证的方法(缺少使用ssh密钥)。 Thus, this would necessarily be a manual process unless you did set up ssh keys between the servers. 因此,除非您确实在服务器之间设置了ssh密钥,否则这将必然是手动过程。 As it does not sound like you have that kind of access to ExampleSiteB.com, this probably isn't acceptable. 由于听起来您不具有对ExampleSiteB.com的访问权限,所以这可能是不可接受的。

Update 2: Since my initial answer turned out to be of little use, I figured I would expand upon one of the above answers. 更新2:由于最初的答案被证明没有多大用处,所以我认为我将扩展以上答案之一。 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() . 我试图找到一种不涉及PECL扩展的解决方案,但是ftp_ssh_connect()并不太幸运。 I recommend trying it, you may have better luck and could forgo the PECL extension route. 我建议您尝试一下,这样可能会更好,并且可以放弃PECL扩展路线。

Sigh, on further reading, it appears ftp_ssh_connect is, understandably, incompatible with the sftp protocol. ftp_ssh_connect是,在进一步阅读中,似乎ftp_ssh_connect与sftp协议不兼容。 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. 但是,我找到了一篇不错的博客文章,内容涉及利用ssh2_connect()ssh2_sftp() (如上一个答案中所述),并认为我会发布该文章以为您提供更多帮助。 It is not as simple as calling the functions for most PHP distributions. 它不像调用大多数PHP发行版的函数那样简单。 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. 在我的系统上,我所要做的就是运行apt-get install libssh2-1-dev libssh2-php ,我能够在php -m输出中找到ssh2。

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. 有一个包含,只要您对网站具有读/写权限,您从中获取文件就可以正常工作,但这只是猜测工作atm,因为我没有任何检查方法。 Good luck though! 祝你好运!

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. 谁告诉您您不能执行此操作,可能会与JavaScript和跨站点脚本浏览器限制混淆,后者限制了从一个域下载的JavaScript不能访问另一个域中的内容。

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). 就是说,如果您使用的PHP对我而言意味着您正在谈论在Web服务器上运行的PHP,则应该能够使用PHP或任何其他脚本或编程语言从SiteB.com下载文件,然后进行更新文件,然后最终通过FTP将文件传输到其他Web服务器(SiteA.com)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM