简体   繁体   中英

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? I am very new to php and i just got to this point and got stuck with this error.

Fatal error: Call to undefined function 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. You have to add them using pecl, see php manual for a good description: http://de1.php.net/manual/en/ssh2.installation.php

I think you'd have an easier time using phpseclib, a pure PHP SFTP implementation . Not only would it make for more portable and easily deployable code than libssh2 but it'd also make for shorter code too:

<?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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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