简体   繁体   English

使用PHP从远程服务器(SFTP)下载sqlite数据库

[英]Download sqlite database from remote server(SFTP) using php

I am currently working on a project where I need to download sqlite database to local folder.So is it possible to download a SQLite database from a webserver, then search the database for content? 我目前正在一个需要将sqlite数据库下载到本地文件夹的项目中,因此可以从Web服务器下载SQLite数据库,然后在数据库中搜索内容吗? I am very new to php and i just got to this point and got stuck with this error. 我对php很陌生,但我到了这一点,并陷入了这个错误。

Fatal error: Call to undefined function ssh2_connect() 致命错误:调用未定义函数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_* function are not per default installed in php core. ssh_*函数不是默认安装在php core中。 You have to add them using pecl, see php manual for a good description: http://de1.php.net/manual/en/ssh2.installation.php 您必须使用pecl来添加它们,请参见php手册以获取详细说明: http ://de1.php.net/manual/zh/ssh2.installation.php

I think you'd have an easier time using phpseclib, a pure PHP SFTP implementation . 我认为使用纯PHP SFTP实现phpseclib会更轻松。 Not only would it make for more portable and easily deployable code than libssh2 but it'd also make for shorter code too: 与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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM