通过 PHP SSH2 从本地上传文件到 SFTP
Upload file from local to SFTP via PHP SSH2
我正在尝试通过 PHP 将本地文件从我的 Mac 上传到 SFTP。我的代码:
$connection = ssh2_connect($server, $port);
if (ssh2_auth_password($connection, $username, $passwd)) {
$sftp = ssh2_sftp($connection);
echo "Connection successful, uploading file now..."."\n";
$file = '/Users/petenaylor/Desktop/diamondexclusive.mp4';
$contents = file_get_contents($file);
file_put_contents("ssh2.sftp://{$sftp}/{$file}", $contents);
}
else {
echo "Unable to authenticate with server"."n";
}
它正常连接,我已检查本地文件位置是否正确,但我收到的错误消息是:
警告:file_get_contents(/Users/petenaylor/Desktop/diamondexclusive.mp4):无法打开流:第 39[=13 行的 /home/test.php 中没有此类文件或目录=]
警告:file_put_contents():无法在 /home/test.[= 中的远程主机上打开 ssh2.sftp://Resource id #3//Users/petenaylor/Desktop/diamondexclusive.mp4 37=] 在第 40 行
警告:file_put_contents(ssh2.sftp://资源 ID #3//Users/petenaylor/Desktop/diamondexclusive.mp4):无法打开流:/home/test 中的操作失败。php 第 40 行
我来自 Filezilla 的日志文件:
Command: put "/Users/petenaylor/Desktop/diamondexclusive.mp4" "diamondexclusive.mp4"
Status: local:/Users/petenaylor/Desktop/diamondexclusive.mp4 => remote:/home/myfarewellnote/web/diamondexclusive.mp4
Trace: FileTransferParseResponse(0)
Trace: CSftpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Status: File transfer successful, transferred 12,661,295 bytes in 111 seconds
Status: Retrieving directory listing of "/home/myfarewellnote/web"...
Trace: CSftpControlSocket::ParseSubcommandResult(0)
Trace: CSftpControlSocket::ListSubcommandResult()
Trace: CSftpControlSocket::SendNextCommand()
Trace: CSftpControlSocket::ListSend()
Command: ls
Status: Listing directory /home/myfarewellnote/web
Trace: CSftpControlSocket::ListParseResponse()
Trace: CSftpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Status: Directory listing of "/home/myfarewellnote/web" successful
在 FileZilla 中,您将名称为 diamondexclusive.mp4
的文件上传到当前远程工作目录,即 /home/myfarewellnote/web
。
因此完整的目标路径是 /home/myfarewellnote/web/diamondexclusive.mp4
。
而在PHP中你将文件上传到/Users/petenaylor/Desktop/diamondexclusive.mp4
(实际上是本地源路径,与服务器无关)。
使用您在 FileZilla 中上传文件的相同路径:
file_put_contents("ssh2.sftp://{$sftp}/home/myfarewellnote/web/diamondexclusive.mp4", $contents);
我正在尝试通过 PHP 将本地文件从我的 Mac 上传到 SFTP。我的代码:
$connection = ssh2_connect($server, $port);
if (ssh2_auth_password($connection, $username, $passwd)) {
$sftp = ssh2_sftp($connection);
echo "Connection successful, uploading file now..."."\n";
$file = '/Users/petenaylor/Desktop/diamondexclusive.mp4';
$contents = file_get_contents($file);
file_put_contents("ssh2.sftp://{$sftp}/{$file}", $contents);
}
else {
echo "Unable to authenticate with server"."n";
}
它正常连接,我已检查本地文件位置是否正确,但我收到的错误消息是:
警告:file_get_contents(/Users/petenaylor/Desktop/diamondexclusive.mp4):无法打开流:第 39[=13 行的 /home/test.php 中没有此类文件或目录=]
警告:file_put_contents():无法在 /home/test.[= 中的远程主机上打开 ssh2.sftp://Resource id #3//Users/petenaylor/Desktop/diamondexclusive.mp4 37=] 在第 40 行
警告:file_put_contents(ssh2.sftp://资源 ID #3//Users/petenaylor/Desktop/diamondexclusive.mp4):无法打开流:/home/test 中的操作失败。php 第 40 行
我来自 Filezilla 的日志文件:
Command: put "/Users/petenaylor/Desktop/diamondexclusive.mp4" "diamondexclusive.mp4"
Status: local:/Users/petenaylor/Desktop/diamondexclusive.mp4 => remote:/home/myfarewellnote/web/diamondexclusive.mp4
Trace: FileTransferParseResponse(0)
Trace: CSftpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Status: File transfer successful, transferred 12,661,295 bytes in 111 seconds
Status: Retrieving directory listing of "/home/myfarewellnote/web"...
Trace: CSftpControlSocket::ParseSubcommandResult(0)
Trace: CSftpControlSocket::ListSubcommandResult()
Trace: CSftpControlSocket::SendNextCommand()
Trace: CSftpControlSocket::ListSend()
Command: ls
Status: Listing directory /home/myfarewellnote/web
Trace: CSftpControlSocket::ListParseResponse()
Trace: CSftpControlSocket::ResetOperation(0)
Trace: CControlSocket::ResetOperation(0)
Status: Directory listing of "/home/myfarewellnote/web" successful
在 FileZilla 中,您将名称为 diamondexclusive.mp4
的文件上传到当前远程工作目录,即 /home/myfarewellnote/web
。
因此完整的目标路径是 /home/myfarewellnote/web/diamondexclusive.mp4
。
而在PHP中你将文件上传到/Users/petenaylor/Desktop/diamondexclusive.mp4
(实际上是本地源路径,与服务器无关)。
使用您在 FileZilla 中上传文件的相同路径:
file_put_contents("ssh2.sftp://{$sftp}/home/myfarewellnote/web/diamondexclusive.mp4", $contents);