ホームページ  >  記事  >  バックエンド開発  >  PHPのfreadバッファの問題

PHPのfreadバッファの問題

WBOY
WBOYオリジナル
2016-06-20 12:37:351252ブラウズ

フォルダーを読み取り、そのすべてのファイルをダウンロードする方法に関する簡単なコードを次に示します。

<?php$host = 'localhost';$port = 22;$username = 'username';$password = 'password';$remoteDir = '/must/be/the/complete/folder/path';$localDir = '/can/be/the/relative/or/absolute/local/path';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://{$stream}/{$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://{$stream}/{$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);}

このコードを に再開することもできます (ディレクトリはコピーされません):

while (false !== ($file = readdir($dirHandle))){    if ($file == "." || $file == "..")        continue;    echo "Copying file: $file\n";    if(!ssh2_scp_recv($connection, $remoteDir . $file, $localDir . $file))        echo "Could not download: ", $remoteDir, $file, "\n";}

リモート フォルダーでフル パスを使用しないと機能しません:

opendir("ssh2.sftp://{$stream}{$remoteDir}")


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。