Here is a small code on how to read the folder and download all its files:
云州網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)自2013年起到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
<?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}")
網(wǎng)頁名稱:phpfreadbuffer問題
文章路徑:http://aaarwkj.com/article0/jjheio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)、營銷型網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)