【问题标题】:I am creating PHP page in which i am downloading .CSV file from SFTP server我正在创建 PHP 页面,我在其中从 SFTP 服务器下载 .CSV 文件
【发布时间】:2015-03-26 12:27:27
【问题描述】:

我想从 SFTP 下载文件,所以我创建了一个看起来像它的页面。

 <?php
     $username = "XYZ";
     $password = "ABC";
     $url ='FTP.abc.COM';
     // Make our connection
     $connection = ssh2_connect($url);
     // Authenticate
     if (!ssh2_auth_password($connection, $username, $password)) throw new Exception('Unable to connect.');
    // Create our SFTP resource
    if (!$sftp = ssh2_sftp($connection)) throw new Exception('Unable to create SFTP connection.');

   $localDir  = '/path/to/your/local/dir';
   $remoteDir = '/path/to/your/remote/dir';

   // download all the files
   $files    = scandir('ssh2.sftp://' . $sftp . $remoteDir);
   if (!empty($files))
    {
     foreach ($files as $file) {
        if ($file != '.' && $file != '..')
        {
           ssh2_scp_recv($connection, "$remoteDir/$file", "$localDir/$file");
        }
    }
}
?>

什么时候我会从浏览器调用这个页面。它会显示错误的样子。

Fatal error: Call to undefined function ssh2_connect() in page.php on line 6

如果这个方法是正确的,那么我应该在这个页面中编辑什么? 如果有另一种方法,那么建议我使用那个方法。提前致谢。

【问题讨论】:

    标签: php ftp sftp ssh2-sftp


    【解决方案1】:

    phpseclib, a pure PHP SFTP implementation 可能会让您获得更好的成功。例如。

    <?php
    include('Net/SFTP.php');
    
    $sftp = new Net_SFTP('www.domain.tld');
    if (!$sftp->login('username', 'password')) {
        exit('Login Failed');
    }
    
    // outputs the contents of filename.remote to the screen
    echo $sftp->get('filename.remote');
    // copies filename.remote to filename.local from the SFTP server
    $sftp->get('filename.remote', 'filename.local');
    ?>
    

    它比 libssh2 有很多优点:

    http://phpseclib.sourceforge.net/ssh/compare.html

    【讨论】:

    • 我已将该库添加到我的项目文件夹中。但我收到了这个错误。注意:在第 2729 行从 C:\xampp\htdocs\tms\admin\Net\SSH2.php 中的套接字读取时出错 注意:连接由 C:\xampp\htdocs\tms\admin\Net\SSH2.php 中的服务器关闭第 1417 行登录失败
    【解决方案2】:

    SSH2 函数不是 PHP 中的标准函数。您必须先安装它们。详情请见http://php.net/manual/en/ref.ssh2.php

    【讨论】:

    • 另请注意,此 PHP 扩展是 libssh2 库的包装器,并假定此库已安装在您的服务器上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多