我正在创建PHP页面,从SFTP服务器下载.CSV文件
我想从SFTP下载文件,所以我创建了一个类似的页面我正在创建PHP页面,从SFTP服务器下载.CSV文件,php,ftp,sftp,ssh2-sftp,Php,Ftp,Sftp,Ssh2 Sftp,我想从SFTP下载文件,所以我创建了一个类似的页面 <?php $username = "XYZ"; $password = "ABC"; $url ='FTP.abc.COM'; // Make our connection $connection = ssh2_connect($url); // Authenticate if (!ssh2_auth_password($connection, $username,
<?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");
}
}
}
?>
如果此方法正确,那么我应该在此页面中编辑什么?
如果有其他方法,请向我推荐该方法。提前感谢。SSH2函数不是PHP中的标准函数。你必须先安装它们。有关更多详细信息,请参阅。您可能会获得更好的成功。例如
与libssh2相比,它有许多优点:
还请注意,此PHP扩展是libssh2库的包装,并假定此库已安装在您的服务器上。我已将该库添加到我的项目文件夹中。但我得到了这个错误。注意:从第2729行的C:\xampp\htdocs\tms\admin\Net\SSH2.php中的套接字读取时出错注意:第1417行的C:\xampp\htdocs\tms\admin\Net\SSH2.php中的服务器关闭的连接登录失败
Fatal error: Call to undefined function ssh2_connect() in page.php on line 6
<?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');
?>