PHP fread,php fread buffer问题

Here is a small code on how to read the folder and download all its files:<?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);}

You can also resume this code to (it will not copy directories):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";}

If you do not use the full path on the remote folder it will not work:opendir("ssh2.sftp://{$stream}{$remoteDir}")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值