您好,登錄后才能下訂單哦!
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("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}")
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。