使用PHP将所有文件从SFTP文件夹下载到本地文件夹
我尝试将所有文件从SFTP文件夹移动到本地文件夹 我使用以下脚本:使用PHP将所有文件从SFTP文件夹下载到本地文件夹,php,sftp,Php,Sftp,我尝试将所有文件从SFTP文件夹移动到本地文件夹 我使用以下脚本: $connection = ssh2_connect('x.x.x.x', 22); if (!ssh2_auth_password($connection, 'User_login', 'User_Pass')) { throw new Exception('Impossible de ce connencter.'); } if (!$sftp = ssh2_sftp($connection)) { th
$connection = ssh2_connect('x.x.x.x', 22);
if (!ssh2_auth_password($connection, 'User_login', 'User_Pass')) {
throw new Exception('Impossible de ce connencter.');
}
if (!$sftp = ssh2_sftp($connection)) {
throw new Exception('Impossible de ce connencter.');
}
$files = array();
$dirHandle = opendir("ssh2.sftp://$sftp/01_Folder/");
while (false !== ($file = readdir($dirHandle))) {
if ($file != '.' && $file != '..') {
$files[] = $file;
}
}
谢谢大家。如果您想将所有文件从SFTP目录下载到本地目录,那么从PHP脚本开始,如果您称之为FTP服务器,请执行以下操作:
添加错误检查 如果您希望将所有文件从SFTP目录下载到本地目录,则在PHP脚本中为本地目录(如果您称之为FTP服务器):
添加错误检查 感兴趣人士的解决方案==>
//connecxion
$connection = ssh2_connect('remote.server.com', 22);
// Authentication
if (!ssh2_auth_password($connection, 'Login_user', 'Password')) {
throw new Exception('Impossible de ce connencter.');
}
// Creation de la source SFTP
if (!$sftp = ssh2_sftp($connection)) {
throw new Exception('Impossible de ce connencter.');
}
$files = array();
$dirHandle = opendir("ssh2.sftp://$sftp/Remote_folder/");
while (false !== ($file = readdir($dirHandle))) {
if ($file != '.' && $file != '..') {
$files[] = $file;
}
}
if (count($files)) {
foreach ($files as $fileName) {
// Dossier Change
if (!$remoteStream = @fopen("ssh2.sftp://$sftp/Remote_folder/$fileName", 'r')) {
throw new Exception("Unable to open remote file: $fileName");
}
// Dossier Local
if (!$localStream = @fopen("/local_folder/$fileName", 'w')) {
throw new Exception("Unable to open local file for writing: /var/www/change_files/$fileName");
}
// Ecriture du dossier change dans le dossier Local
$read = 0;
$fileSize = filesize("ssh2.sftp://$sftp/Remote_folder/$fileName");
while ($read < $fileSize && ($buffer = fread($remoteStream, $fileSize - $read))) {
$read += strlen($buffer);
// Ecriture du dossier
if (fwrite($localStream, $buffer) === FALSE) {
throw new Exception("Unable to write to local file: /local_folder/$fileName");
}
}
// Fermeture des Connexions
fclose($localStream);
fclose($remoteStream);
}
}
感兴趣人士的解决方案==>
//connecxion
$connection = ssh2_connect('remote.server.com', 22);
// Authentication
if (!ssh2_auth_password($connection, 'Login_user', 'Password')) {
throw new Exception('Impossible de ce connencter.');
}
// Creation de la source SFTP
if (!$sftp = ssh2_sftp($connection)) {
throw new Exception('Impossible de ce connencter.');
}
$files = array();
$dirHandle = opendir("ssh2.sftp://$sftp/Remote_folder/");
while (false !== ($file = readdir($dirHandle))) {
if ($file != '.' && $file != '..') {
$files[] = $file;
}
}
if (count($files)) {
foreach ($files as $fileName) {
// Dossier Change
if (!$remoteStream = @fopen("ssh2.sftp://$sftp/Remote_folder/$fileName", 'r')) {
throw new Exception("Unable to open remote file: $fileName");
}
// Dossier Local
if (!$localStream = @fopen("/local_folder/$fileName", 'w')) {
throw new Exception("Unable to open local file for writing: /var/www/change_files/$fileName");
}
// Ecriture du dossier change dans le dossier Local
$read = 0;
$fileSize = filesize("ssh2.sftp://$sftp/Remote_folder/$fileName");
while ($read < $fileSize && ($buffer = fread($remoteStream, $fileSize - $read))) {
$read += strlen($buffer);
// Ecriture du dossier
if (fwrite($localStream, $buffer) === FALSE) {
throw new Exception("Unable to write to local file: /local_folder/$fileName");
}
}
// Fermeture des Connexions
fclose($localStream);
fclose($remoteStream);
}
}
可以使用函数从Php运行rsync。它将询问您远程服务器的用户名和密码。可以使用命令行工具绕过该选项。它允许非交互式登录。以下命令使用sshpass运行rsync:
rsync --rsh="sshpass -p myPassword ssh -l username" server.example.com:/var/www/html/ /backup/
可以使用exec函数从Php运行该命令您可以使用该函数从Php运行rsync。它将询问您远程服务器的用户名和密码。可以使用命令行工具绕过该选项。它允许非交互式登录。以下命令使用sshpass运行rsync:
rsync --rsh="sshpass -p myPassword ssh -l username" server.example.com:/var/www/html/ /backup/
该命令可以使用exec函数从Php运行您不能使用SFTP将文件放在FTP服务器上,这是两件不同的事情。感谢您的回答,FTP服务器是本地流,他启动脚本来远程SFTP服务器以获取文件。那么您问题中的另一个远程ftp服务器如何与您评论中的本地流相匹配呢?感谢各位,我找到了解决方案==>您不能使用SFTP将文件放在ftp服务器上,这是两件不同的事情。感谢您的回答,ftp服务器是本地流,他启动脚本远程访问SFTP服务器以获取文件。那么您问题中的另一个远程ftp服务器如何与您评论中的本地流相匹配呢?谢谢各位我找到了解决方案==>谢谢Nadir先生,这是一个真正的解决方案我更喜欢这种方法Y但它与SFTP无关,您的问题是关于什么的。谢谢Nadir先生,这是一个真正的解决方案,我更喜欢这种方法,但它与SFTP无关,你的问题是关于什么的。