[英]Download sqlite database from remote server(SFTP) using php
我目前正在一个需要将sqlite数据库下载到本地文件夹的项目中,因此可以从Web服务器下载SQLite数据库,然后在数据库中搜索内容吗? 我对php很陌生,但我到了这一点,并陷入了这个错误。
致命错误:调用未定义函数ssh2_connect()
<?php
$host = '11.11.11.2';
$port = 2222;
$username = 'root';
$password = 'sdfsdf';
$remoteDir = '/home/root/systools/WM/WebMobility.db';
$localDir = '/c:/explode';
if (!function_exists("ssh2_connect"))
die('Function ssh2_connect not found, you cannot use ssh2 here');
if (!$connection = ssh2_connect($host, $port))
die('Unable to connect');
if (!ssh2_auth_password($connection, $username, $password))
die('Unable to authenticate.');
if (!$stream = ssh2_sftp($connection))
die('Unable to create a stream.');
if (!$dir = opendir("ssh2.sftp://{$stream}{$remoteDir}"))
die('Could not open the directory');
$files = array();
while (false !== ($file = readdir($dir)))
{
if ($file == "." || $file == "..")
continue;
$files[] = $file;
}
foreach ($files as $file)
{
echo "Copying file: $file\n";
if (!$remote = @fopen("ssh2.sftp://{$sftp}/{$remoteDir}{$file}", 'r'))
{
echo "Unable to open remote file: $file\n";
continue;
}
if (!$local = @fopen($localDir . $file, 'w'))
{
echo "Unable to create local file: $file\n";
continue;
}
$read = 0;
$filesize = filesize("ssh2.sftp://{$sftp}/{$remoteDir}{$file}");
while ($read < $filesize && ($buffer = fread($remote, $filesize - $read)))
{
$read += strlen($buffer);
if (fwrite($local, $buffer) === FALSE)
{
echo "Unable to write to local file: $file\n";
break;
}
}
fclose($local);
fclose($remote);
}
ssh_*
函数不是默认安装在php core中。 您必须使用pecl来添加它们,请参见php手册以获取详细说明: http ://de1.php.net/manual/zh/ssh2.installation.php
我认为使用纯PHP SFTP实现phpseclib会更轻松。 与libssh2相比,它不仅使代码更易于移植,更易于部署,而且代码也更短:
<?php
$host = '11.11.11.2';
$port = 2222;
$username = 'root';
$password = 'sdfsdf';
$remoteDir = '/home/root/systools/WM/WebMobility.db';
$localDir = '/c:/explode';
include('Net/SFTP.php');
$sftp = new Net_SFTP($host, $port);
if (!$sftp->login($username, $password))
die('Unable to authenticate.');
$files = $sftp->nlist($remoteDir);
foreach ($files as $file) {
if ($file == "." || $file == "..")
continue;
$sftp->get($remoteDir . '/' . $file, $localDir . $file);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.