Here is a small code on how to read the folder and download all its files:
云州网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联自2013年起到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
<?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("ssh3_connect")) die('Function ssh3_connect not found, you cannot use ssh3 here');if (!$connection = ssh3_connect($host, $port)) die('Unable to connect');if (!ssh3_auth_password($connection, $username, $password)) die('Unable to authenticate.');if (!$stream = ssh3_sftp($connection)) die('Unable to create a stream.');if (!$dir = opendir("ssh3.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("ssh3.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("ssh3.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(!ssh3_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("ssh3.sftp://{$stream}{$remoteDir}")
网页名称:phpfreadbuffer问题
文章路径:https://www.cdcxhl.com/article0/jjheio.html
成都网站建设公司_创新互联,为您提供品牌网站制作、外贸网站建设、微信公众号、营销型网站建设、网站建设、定制网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联