通过sftp从远程服务器下载PHP [英] PHP download from remote server via sftp
问题描述
这可能是以前问过的,我是PHP的新手,我正在尝试学习尽可能多的东西,但这确实使我感到困惑.
This may have been asked before, I'm new to PHP and I'm trying to learn as much as I can, but this has really thrown me.
基本上我想知道的是,我将如何使用PHP代码来获取它,以将所有内容从远程服务器下载到本地.它正在下载所有内容,而不仅仅是下载我所卡住的一个文件.因此,请有人可以向我展示/解释我将如何做吗?
Basically what I want to know is, how would I use PHP code to get it to download everything from a remote server to a local location. It's getting it to download everything not just one file that I'm stuck on. So please can someone show/explain to me how I would do this?
到目前为止我所拥有的:
What I've got so far:
<?php
$connection - ssh2_connect('example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$remote_dir="/remote_dir/";
$local_dir="/local_dir/";
$remote ="$remote_dir";
$stream = ssh2_exec($connection, $remote);
stream_set_blocking($stream,true);
$command=fread($stream,4096);
$array=explode(\n,$command);
$total_files=sizeof($array);
for($i=0;$i<$total_files;$i+++){
$file_name=trim($array[$i]);
if($file_name!=''{
$remote_file=$remote_dir.$file_name;
$local_file=$local_dir.$file_name;
if(ssh2_scp_recv($connection, $remote_file,$local_file)){
echo "File ".$file_name." was copied to $local_dir<br />";
}
}
}
fclose($stream);
?>
我认为我的$ remote ="$ remote_dir";是错误的,老实说,当我想要整个目录时,我已经有了$ filename,这就是我到目前为止所拥有的全部.
I think my $remote ="$remote_dir"; is wrong, and to be honest I've got $filename when I want the whole directory, this is all I have so far.
推荐答案
以下是有关如何读取文件夹并下载其所有文件的小代码:
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}")
这篇关于通过sftp从远程服务器下载PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!