繁体   English   中英

如何使用PHP将一个文件夹本地文件复制到SFTP服务器

[英]How to Copy one folder local file to SFTP server using PHP

这是我的代码,用于将单个文件从本地发送到SFTP服务器。 单个文件发送成功。 我想从本地向SFTP服务器发送一个文件夹,但是我不知道如何发送一个文件夹。 我需要帮助

<?php
    $src = 'xxxxxxx';
    $filename = 'test.txt';
    $dest = 'xxxxxxxx'.$filename;

    // set up sftp ssh-sftp connection
    $connection = ssh2_connect('xxxxxx', 22);
    ssh2_auth_password($connection, 'username', 'password');

    // Create SFTP session
    $sftp = ssh2_sftp($connection);

    $sftpStream = @fopen('ssh2.sftp://'.$sftp.$dest, 'w');

    try {
        if (!$sftpStream) {
            throw new Exception("Could not open remote file: $dest");
        }
        $data_to_send = @file_get_contents($src);

        if ($data_to_send === false) {
            throw new Exception("Could not open local file: $src.");
        }      
        if (@fwrite($sftpStream, $data_to_send) === false) {
            throw new Exception("Could not send data from file: $src.");
        } else {
            //Upload was successful, post-upload actions go here...
        } 
        fclose($sftpStream);                   
    } catch (Exception $e) {
        error_log('Exception: ' . $e->getMessage());
        fclose($sftpStream);
    }
    ?>

php中的ssh2_scp_send似乎仅支持发送文件。 也许这些方式有效:

1.compress folder, send the compressed file
2.use ssh2 to mkdir, then send each file iteratively
3.try with system() function to excute cmd line order "scp -r folder_path user@host:target_path"

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM