<i id='tEB0j'><tr id='tEB0j'><dt id='tEB0j'><q id='tEB0j'><span id='tEB0j'><b id='tEB0j'><form id='tEB0j'><ins id='tEB0j'></ins><ul id='tEB0j'></ul><sub id='tEB0j'></sub></form><legend id='tEB0j'></legend><bdo id='tEB0j'><pre id='tEB0j'><center id='tEB0j'></center></pre></bdo></b><th id='tEB0j'></th></span></q></dt></tr></i><div id='tEB0j'><tfoot id='tEB0j'></tfoot><dl id='tEB0j'><fieldset id='tEB0j'></fieldset></dl></div>

    <tfoot id='tEB0j'></tfoot>
  • <legend id='tEB0j'><style id='tEB0j'><dir id='tEB0j'><q id='tEB0j'></q></dir></style></legend>

    <small id='tEB0j'></small><noframes id='tEB0j'>

      • <bdo id='tEB0j'></bdo><ul id='tEB0j'></ul>

        如何使用 PHP 从 SFTP 服务器下载文件

        时间:2023-08-20
          <bdo id='Mxevo'></bdo><ul id='Mxevo'></ul>

            <small id='Mxevo'></small><noframes id='Mxevo'>

          1. <i id='Mxevo'><tr id='Mxevo'><dt id='Mxevo'><q id='Mxevo'><span id='Mxevo'><b id='Mxevo'><form id='Mxevo'><ins id='Mxevo'></ins><ul id='Mxevo'></ul><sub id='Mxevo'></sub></form><legend id='Mxevo'></legend><bdo id='Mxevo'><pre id='Mxevo'><center id='Mxevo'></center></pre></bdo></b><th id='Mxevo'></th></span></q></dt></tr></i><div id='Mxevo'><tfoot id='Mxevo'></tfoot><dl id='Mxevo'><fieldset id='Mxevo'></fieldset></dl></div>
          2. <tfoot id='Mxevo'></tfoot>

                <legend id='Mxevo'><style id='Mxevo'><dir id='Mxevo'><q id='Mxevo'></q></dir></style></legend>
                    <tbody id='Mxevo'></tbody>
                • 本文介绍了如何使用 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 服务器,然后使用 readfile() 从 Web 服务器到用户的本地磁盘.但这意味着两个文件传输,如果文件很大,我想这会很慢.

                  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?

                  为任何回复干杯!

                  推荐答案

                  您不需要任何 php 来允许用户直接从 SFTP 服务器下载,如果您将文件的直接链接添加到您的 html(即下载文本).当然,如果您不想公开 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 中提取文件,根据定义,您必须先将文件下载到服务器,然后再将其发送回用户浏览器.

                  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 服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP中的安全文件下载,未经许可拒绝用户 下一篇:产生真实单词的词干算法

                  相关文章

                  1. <i id='V62wf'><tr id='V62wf'><dt id='V62wf'><q id='V62wf'><span id='V62wf'><b id='V62wf'><form id='V62wf'><ins id='V62wf'></ins><ul id='V62wf'></ul><sub id='V62wf'></sub></form><legend id='V62wf'></legend><bdo id='V62wf'><pre id='V62wf'><center id='V62wf'></center></pre></bdo></b><th id='V62wf'></th></span></q></dt></tr></i><div id='V62wf'><tfoot id='V62wf'></tfoot><dl id='V62wf'><fieldset id='V62wf'></fieldset></dl></div>
                    <legend id='V62wf'><style id='V62wf'><dir id='V62wf'><q id='V62wf'></q></dir></style></legend>
                    1. <small id='V62wf'></small><noframes id='V62wf'>

                    2. <tfoot id='V62wf'></tfoot>
                        <bdo id='V62wf'></bdo><ul id='V62wf'></ul>