如何使用PHP从SFTP服务器下载文件 [英] How to download a file from an SFTP server using PHP

查看:189
本文介绍了如何使用PHP从SFTP服务器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望允许用户直接从sftp服务器下载文件,但在浏览器中。

I'm looking to allow a user to download a file directly from an sftp server, but in the browser.

我找到了读取文件的方法并回显字符串(使用ssh2.sftp或phpseclib的连接),但是我需要下载,而不是读取。

I've found methods to read the file and echo the string (connections using ssh2.sftp or phpseclib) but I need to download, rather than read.

此外,我看到了解决方案,建议从sftp服务器到Web服务器,然后从web服务器使用readfile()到用户的本地磁盘。但是这意味着两个文件传输,如果文件很大,我会想到这会很慢。

Also, I've seen solutions that suggest downloading from the sftp server to the web server, then use readfile() from the web server to the user's local disk. But this means two file transfers, and if the file is large I imagine this would be slow.

你可以直接从sftp下载用户的磁盘?

Can you download directly from sftp to the user's disk?

欢迎您的回复!

推荐答案

如果您直接将文件添加到html(即下载文本)中,则不需要任何php即可直接从SFTP服务器下载用户。当然,如果您不想将ftp服务器的凭据公开,那么这将无法正常工作。

You don't need any php in order to allow the user to download directly from the SFTP server, if you add a direct link to the file to your html (i.e. Download Text). Of course this won't work if you don't want to make the credentials for the ftp server public.

如果您希望从SFTP中提取文件,您的服务器,您通过deffinition,必须将文件下载到服务器,然后再将其发送回用户浏览器。

If you are looking to pull the file from the SFTP through your server, you, by deffinition, must download the file to the server before sending it back out to the users browser.

为此,有很多很多解决方案。最少的开销可能来自使用
phpseclib 如下

For this there are many, many solutions. The least overhead would probably come from using phpseclib as below

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

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

//adds the proper headers to tell browser to download rather than display
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"filename.remote\""); 

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

不幸的是,如果文件大于服务器/ php配置在内存中允许的文件,那么这个很好的原因问题。

Unfortunately, if the file is larger than is allowed in memory by your server/php configuration, then this well cause problems.

如果你想进一步,你可以尝试

If you want to take it a step further, you might try

//adds the proper headers to tell browser to download rather than display
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"filename.remote\""); 

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "sftp://full_file_url.file"); #input
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[username]:$_FTP[password]");
curl_exec($curl);
curl_close($curl);

有关使用cURL的更多信息,请参见 PHP手册文档。使用curl_exec()将CURLOPT_RETURNTRANSFER选项设置为true可以使curl将输出(文件)直接发送到浏览器。

More information on using cURL can be found in the PHP Manual Documentation. Using curl_exec() without setting the CURLOPT_RETURNTRANSFER option to true causes curl to send the output (the file) directly to the browser.

这篇关于如何使用PHP从SFTP服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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