phpseclib put returns true 但文件未上传
phpseclib put returns true but file is not uploaded
使用 PHP 插件 phpseclib v2.0.18 和 PHP 7.3,当我尝试将文件上传到 SFTP 服务器时,put()
方法 returns true , 但文件从未上传。
日志显示一切正常。
我的代码:
foreach ($schedules as $idx => $schedule)
{
$company = \Company::getItem($schedule->id_login);
$outgoing_path = concat_path(BASE_PATH, 'pdf', 'file_schedules', $company->cif, 'outgoing', date('Ymd'));
if (!is_dir($outgoing_path)) create_folder($outgoing_path, 0775, true);
if ( ! ($sftp = new SFTP($schedule->host, $schedule->port)))
{
static::logError('No se pudo iniciar la conexión.', array($schedule->id_login, $company->name, $company->cif));
continue;
}
if (!$sftp->login($schedule->user, $schedule->password))
{
static::logError('No se pudo hacer login en el servidor remoto.', array($schedule->id_login, $company->name, $company->cif));
continue;
}
unset($schedule->password);
if (!$sftp->chdir(concat_path($schedule->path, 'outgoing')))
{
static::logError('No se pudo acceder al directorio.', $schedule);
}
$dir_iterator = new \DirectoryIterator($outgoing_path);
$count = 0;
foreach ($dir_iterator as $file)
{
if ($file->isDot()) continue;
if ($sftp->put($file->getPathname(), concat_path($sftp->pwd(), $file->getFilename(), SFTP::SOURCE_LOCAL_FILE)))
{
$count += 1;
}
}
static::logInfo("$count archivos exportados.");
}
登录正常。
本地和远程路径和文件名都很好。
下载文件的类似代码工作正常。
文件权限没问题。
我做错了什么?
日志输出:
-> NET_SFTP_INIT (0s)
00000000 00:00:00:03 ....
<- NET_SFTP_VERSION (0.008s)
00000000 00:00:00:03:00:00:00:18:70:6f:73:69:78:2d:72:65 ........posix-re
00000010 6e:61:6d:65:40:6f:70:65:6e:73:73:68:2e:63:6f:6d name@openssh.com
00000020 00:00:00:01:31:00:00:00:13:73:74:61:74:76:66:73 ....1....statvfs
00000030 40:6f:70:65:6e:73:73:68:2e:63:6f:6d:00:00:00:01 @openssh.com....
00000040 32:00:00:00:14:66:73:74:61:74:76:66:73:40:6f:70 2....fstatvfs@op
00000050 65:6e:73:73:68:2e:63:6f:6d:00:00:00:01:32:00:00 enssh.com....2..
00000060 00:14:68:61:72:64:6c:69:6e:6b:40:6f:70:65:6e:73 ..hardlink@opens
00000070 73:68:2e:63:6f:6d:00:00:00:01:31:00:00:00:11:66 sh.com....1....f
00000080 73:79:6e:63:40:6f:70:65:6e:73:73:68:2e:63:6f:6d sync@openssh.com
00000090 00:00:00:01:31 ....1
-> NET_SFTP_REALPATH (0s)
00000000 00:00:00:01:2e .....
<- NET_SFTP_NAME (0.003s)
00000000 00:00:00:01:00:00:00:0c:2f:55:73:65:72:73:2f:6a ......../Users/j
00000010 6f:72:67:65:00:00:00:0c:2f:55:73:65:72:73:2f:6a orge..../Users/j
00000020 6f:72:67:65:00:00:00:00 orge....
-> NET_SFTP_OPENDIR (0s)
00000000 00:00:00:15:2f:55:73:65:72:73:2f:6a:6f:72:67:65 ..../Users/jorge
00000010 2f:6f:75:74:67:6f:69:6e:67 /outgoing
<- NET_SFTP_HANDLE (0.0032s)
00000000 00:00:00:04:00:00:00:00 ........
-> NET_SFTP_CLOSE (0s)
00000000 00:00:00:04:00:00:00:00 ........
<- NET_SFTP_STATUS (0.0031s)
00000000 00:00:00:00:00:00:00:07:53:75:63:63:65:73:73:00 ........Success.
00000010 00:00:00 ...
-> NET_SFTP_OPEN (0.0001s)
00000000 00:00:00:6e:2f:55:73:65:72:73:2f:6a:6f:72:67:65 ...n/Users/jorge
00000010 2f:4d:45:47:41:73:79:6e:63:2f:77:6f:72:6b:73:70 /MEGAsync/worksp
00000020 61:63:65:2f:74:65:73:6f:72:69:7a:61:2f:6d:69:6c ace/tesoriza/mil
00000030 65:73:74:6f:6e:65:73:2f:70:64:66:2f:66:69:6c:65 estones/pdf/file
00000040 5f:73:63:68:65:64:75:6c:65:73:2f:41:38:36:32:31 _schedules/A8621
00000050 32:34:32:30:2f:6f:75:74:67:6f:69:6e:67:2f:32:30 2420/outgoing/20
00000060 31:39:31:30:30:37:2f:50:72:75:65:62:61:2e:78:6c 191007/Prueba.xl
00000070 73:78:00:00:00:1a:00:00:00:00 sx........
<- NET_SFTP_HANDLE (0.0047s)
00000000 00:00:00:04:00:00:00:00 ........
-> NET_SFTP_WRITE (0s)
00000000 00:00:00:04:00:00:00:00:00:00:00:00:00:00:00:00 ................
00000010 00:00:00:23:2f:55:73:65:72:73:2f:6a:6f:72:67:65 ...#/Users/jorge
00000020 2f:6f:75:74:67:6f:69:6e:67:2f:50:72:75:65:62:61 /outgoing/Prueba
00000030 2e:78:6c:73:78:2f:31 .xlsx/1
<- NET_SFTP_STATUS (0.0032s)
00000000 00:00:00:00:00:00:00:07:53:75:63:63:65:73:73:00 ........Success.
00000010 00:00:00 ...
-> NET_SFTP_CLOSE (0s)
00000000 00:00:00:04:00:00:00:00 ........
<- NET_SFTP_STATUS (0.0031s)
00000000 00:00:00:00:00:00:00:07:53:75:63:63:65:73:73:00 ........Success.
00000010 00:00:00 ...
好像还可以。
来自您的代码:
if ($sftp->put($file->getPathname(), concat_path($sftp->pwd(), $file->getFilename(), SFTP::SOURCE_LOCAL_FILE)))
{
$count += 1;
}
让我们分解一下。所以你这样做:
$sftp->put(
$file->getPathname(),
concat_path($sftp->pwd(), $file->getFilename(), SFTP::SOURCE_LOCAL_FILE)
);
我知道 concat_path
做了什么,但我猜你的括号位置有误。我的猜测是您想这样做:
$sftp->put(
$file->getPathname(),
concat_path($sftp->pwd(), $file->getFilename()),
SFTP::SOURCE_LOCAL_FILE
);
无论如何,你在哪里找文件?根据您的日志,它们应该出现在 /Users/jorge/MEGAsync/workspace/tesoriza/milestones/pdf/file_schedules/A86212420/outgoing/20191007/Prueba.xlsx
。那是你要找的地方吗?
使用 PHP 插件 phpseclib v2.0.18 和 PHP 7.3,当我尝试将文件上传到 SFTP 服务器时,put()
方法 returns true , 但文件从未上传。
日志显示一切正常。
我的代码:
foreach ($schedules as $idx => $schedule)
{
$company = \Company::getItem($schedule->id_login);
$outgoing_path = concat_path(BASE_PATH, 'pdf', 'file_schedules', $company->cif, 'outgoing', date('Ymd'));
if (!is_dir($outgoing_path)) create_folder($outgoing_path, 0775, true);
if ( ! ($sftp = new SFTP($schedule->host, $schedule->port)))
{
static::logError('No se pudo iniciar la conexión.', array($schedule->id_login, $company->name, $company->cif));
continue;
}
if (!$sftp->login($schedule->user, $schedule->password))
{
static::logError('No se pudo hacer login en el servidor remoto.', array($schedule->id_login, $company->name, $company->cif));
continue;
}
unset($schedule->password);
if (!$sftp->chdir(concat_path($schedule->path, 'outgoing')))
{
static::logError('No se pudo acceder al directorio.', $schedule);
}
$dir_iterator = new \DirectoryIterator($outgoing_path);
$count = 0;
foreach ($dir_iterator as $file)
{
if ($file->isDot()) continue;
if ($sftp->put($file->getPathname(), concat_path($sftp->pwd(), $file->getFilename(), SFTP::SOURCE_LOCAL_FILE)))
{
$count += 1;
}
}
static::logInfo("$count archivos exportados.");
}
登录正常。 本地和远程路径和文件名都很好。 下载文件的类似代码工作正常。 文件权限没问题。
我做错了什么?
日志输出:
-> NET_SFTP_INIT (0s)
00000000 00:00:00:03 ....
<- NET_SFTP_VERSION (0.008s)
00000000 00:00:00:03:00:00:00:18:70:6f:73:69:78:2d:72:65 ........posix-re
00000010 6e:61:6d:65:40:6f:70:65:6e:73:73:68:2e:63:6f:6d name@openssh.com
00000020 00:00:00:01:31:00:00:00:13:73:74:61:74:76:66:73 ....1....statvfs
00000030 40:6f:70:65:6e:73:73:68:2e:63:6f:6d:00:00:00:01 @openssh.com....
00000040 32:00:00:00:14:66:73:74:61:74:76:66:73:40:6f:70 2....fstatvfs@op
00000050 65:6e:73:73:68:2e:63:6f:6d:00:00:00:01:32:00:00 enssh.com....2..
00000060 00:14:68:61:72:64:6c:69:6e:6b:40:6f:70:65:6e:73 ..hardlink@opens
00000070 73:68:2e:63:6f:6d:00:00:00:01:31:00:00:00:11:66 sh.com....1....f
00000080 73:79:6e:63:40:6f:70:65:6e:73:73:68:2e:63:6f:6d sync@openssh.com
00000090 00:00:00:01:31 ....1
-> NET_SFTP_REALPATH (0s)
00000000 00:00:00:01:2e .....
<- NET_SFTP_NAME (0.003s)
00000000 00:00:00:01:00:00:00:0c:2f:55:73:65:72:73:2f:6a ......../Users/j
00000010 6f:72:67:65:00:00:00:0c:2f:55:73:65:72:73:2f:6a orge..../Users/j
00000020 6f:72:67:65:00:00:00:00 orge....
-> NET_SFTP_OPENDIR (0s)
00000000 00:00:00:15:2f:55:73:65:72:73:2f:6a:6f:72:67:65 ..../Users/jorge
00000010 2f:6f:75:74:67:6f:69:6e:67 /outgoing
<- NET_SFTP_HANDLE (0.0032s)
00000000 00:00:00:04:00:00:00:00 ........
-> NET_SFTP_CLOSE (0s)
00000000 00:00:00:04:00:00:00:00 ........
<- NET_SFTP_STATUS (0.0031s)
00000000 00:00:00:00:00:00:00:07:53:75:63:63:65:73:73:00 ........Success.
00000010 00:00:00 ...
-> NET_SFTP_OPEN (0.0001s)
00000000 00:00:00:6e:2f:55:73:65:72:73:2f:6a:6f:72:67:65 ...n/Users/jorge
00000010 2f:4d:45:47:41:73:79:6e:63:2f:77:6f:72:6b:73:70 /MEGAsync/worksp
00000020 61:63:65:2f:74:65:73:6f:72:69:7a:61:2f:6d:69:6c ace/tesoriza/mil
00000030 65:73:74:6f:6e:65:73:2f:70:64:66:2f:66:69:6c:65 estones/pdf/file
00000040 5f:73:63:68:65:64:75:6c:65:73:2f:41:38:36:32:31 _schedules/A8621
00000050 32:34:32:30:2f:6f:75:74:67:6f:69:6e:67:2f:32:30 2420/outgoing/20
00000060 31:39:31:30:30:37:2f:50:72:75:65:62:61:2e:78:6c 191007/Prueba.xl
00000070 73:78:00:00:00:1a:00:00:00:00 sx........
<- NET_SFTP_HANDLE (0.0047s)
00000000 00:00:00:04:00:00:00:00 ........
-> NET_SFTP_WRITE (0s)
00000000 00:00:00:04:00:00:00:00:00:00:00:00:00:00:00:00 ................
00000010 00:00:00:23:2f:55:73:65:72:73:2f:6a:6f:72:67:65 ...#/Users/jorge
00000020 2f:6f:75:74:67:6f:69:6e:67:2f:50:72:75:65:62:61 /outgoing/Prueba
00000030 2e:78:6c:73:78:2f:31 .xlsx/1
<- NET_SFTP_STATUS (0.0032s)
00000000 00:00:00:00:00:00:00:07:53:75:63:63:65:73:73:00 ........Success.
00000010 00:00:00 ...
-> NET_SFTP_CLOSE (0s)
00000000 00:00:00:04:00:00:00:00 ........
<- NET_SFTP_STATUS (0.0031s)
00000000 00:00:00:00:00:00:00:07:53:75:63:63:65:73:73:00 ........Success.
00000010 00:00:00 ...
好像还可以。
来自您的代码:
if ($sftp->put($file->getPathname(), concat_path($sftp->pwd(), $file->getFilename(), SFTP::SOURCE_LOCAL_FILE)))
{
$count += 1;
}
让我们分解一下。所以你这样做:
$sftp->put(
$file->getPathname(),
concat_path($sftp->pwd(), $file->getFilename(), SFTP::SOURCE_LOCAL_FILE)
);
我知道 concat_path
做了什么,但我猜你的括号位置有误。我的猜测是您想这样做:
$sftp->put(
$file->getPathname(),
concat_path($sftp->pwd(), $file->getFilename()),
SFTP::SOURCE_LOCAL_FILE
);
无论如何,你在哪里找文件?根据您的日志,它们应该出现在 /Users/jorge/MEGAsync/workspace/tesoriza/milestones/pdf/file_schedules/A86212420/outgoing/20191007/Prueba.xlsx
。那是你要找的地方吗?