我正在创建PHP页面,从SFTP服务器下载.CSV文件


I am creating PHP page in which i am downloading .CSV file from SFTP server

我想从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

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

使用phpseclib,一个纯PHP SFTP实现,您可能会获得更好的成功。例如

<?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

SSH2函数不是PHP中的标准函数。你必须先安装它们。看见http://php.net/manual/en/ref.ssh2.php了解更多详细信息。