简体   繁体   English

我正在尝试使用 sftp 连接从服务器下载文件并将其保存在我的服务器上

[英]I'm trying to download a file from the server using sftp connection and save it on my server

I'm trying to download a file from the server using sftp connection and save it on my server but the file is empty.我正在尝试使用 sftp 连接从服务器下载文件并将其保存在我的服务器上,但文件为空。

$connection = ssh2_connect('{my server ip}', 22);
ssh2_auth_password($connection, '{user}', '{password}');
$sftp = ssh2_sftp($connection);
$file = "test.txt";
$target_path = "ssh2.sftp://$sftp/public_html/$file";
$stream = fopen($target_path, 'r');
fopen($file, "w");
fclose($stream);

You never copy the data between the opened files/handles.您永远不会在打开的文件/句柄之间复制数据。 Easy way to do that is using stream_copy_to_stream :简单的方法是使用stream_copy_to_stream

$local_stream = fopen($file, "w");
stream_copy_to_stream($stream, $local_stream);
fclose($local_stream);

As @KenLee commented, if your server supports SCP too (most do), you can replace 6 lines of the code with simple ssh2_scp_recv :正如@KenLee 评论的那样,如果您的服务器也支持 SCP(大多数都支持),您可以用简单的ssh2_scp_recv替换 6 行代码:

ssh2_scp_recv($connection, "/public_html/$file", $file);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用 PHP readfile() 或 SFTP 连接通过网络应用程序从我的服务器下载文件吗? - Should I use PHP readfile() or SFTP connection to download file from my server through a web app? 是否可以从一台服务器(sftp)下载文件并将其使用php上传到我的服务器? - Is it possible to download a file from one server (sftp) and upload it to my server using php? 如何使用PHP从SFTP服务器下载文件 - How to download a file from an SFTP server using PHP 如何使用ftp从服务器下载文件以及如何将该文件保存在本地文件夹中 - How to download a file from server using ftp and how to save that file in my local folder 如何将文件从Sftp服务器下载到本地计算机 - How to download file from Sftp server to local machine 我正在尝试使用php从mysql从我的域服务器到android应用程序中的数据获取数据 - I'm trying to fetch data from mysql from my domain server to android application using php 尝试使用PHP中的cURL从SFTP服务器下载文件时,权限被拒绝 - Permission denied when tring to download file from SFTP server using cURL in PHP 使用PHP从远程服务器(SFTP)下载sqlite数据库 - Download sqlite database from remote server(SFTP) using php 我正在使用Zend Studio for Eclipse,并且试图保存我的文件,但是失败了! - I'm using Zend Studio for Eclipse and I'm trying to save my file but it failed! 通过 sftp 从远程服务器下载 PHP - PHP download from remote server via sftp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM