dongshan4878 2015-05-29 11:38
浏览 84
已采纳

使用php在两个Debian服务器之间复制文件

My php website is hosted in Debain Machine and I want to move a file from that Machine to another Debain which is connected through VPN.

I tried shell_exec and scp , as mentioned here.

<?php
    $output = shell_exec('scp file1.txt dvader@deathstar.com:somedir');
    echo "<pre>$output</pre>";
?>

I also tried using SFTP

<?php

class SFTPConnection
{
    private $connection;
    private $sftp;

    public function __construct($host, $port=22)
    {
        $this->connection = @ssh2_connect($host, $port);
        if (! $this->connection)
            throw new Exception("Could not connect to $host on port $port.");
    }

    public function login($username, $password)
    {
        if (! @ssh2_auth_password($this->connection, $username, $password))
            throw new Exception("Could not authenticate with username $username " .
                                "and password $password.");

        $this->sftp = @ssh2_sftp($this->connection);
        if (! $this->sftp)
            throw new Exception("Could not initialize SFTP subsystem.");
    }

    public function uploadFile($local_file, $remote_file)
    {
        $sftp = $this->sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');

        if (! $stream)
            throw new Exception("Could not open file: $remote_file");

        $data_to_send = @file_get_contents($local_file);
        if ($data_to_send === false)
            throw new Exception("Could not open local file: $local_file.");

        if (@fwrite($stream, $data_to_send) === false)
            throw new Exception("Could not send data from file: $local_file.");

        @fclose($stream);
    }
}

try
{
    $sftp = new SFTPConnection("localhost", 22);
    $sftp->login("username", "password");
    $sftp->uploadFile("/tmp/to_be_sent", "/tmp/to_be_received");
}
catch (Exception $e)
{
    echo $e->getMessage() . "
";
}

?>

Simply My problem is that I am not able to move a file from one machine, where my php applicaton is working , to another machine which is connected through VPN.

  • 写回答

1条回答 默认 最新

  • dsepcxw181184853 2015-06-01 07:52
    关注

    I installed proftpd and created a user as mentioned here,

    Please note default port is 21. And make sure you restart proftpd:

    service proftpd restart
    

    The code I used to upload a file is:

    index.php

    <?php
    ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);
    $file = 'a.txt';
    $remote_file = 'b.txt';
    $conn_id = ftp_connect('www.xxx.com',21);
    $login_result = ftp_login($conn_id, "username","password");
    if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
     echo "successfully uploaded $file
    ";
    } else {
     echo "There was a problem while uploading $file
    ";
     die();
    }
    ftp_close($conn_id);
    ?>
    

    Here a.txt is present in the same directory of index.php.

    It will copy the file to the folder you mentioned for that particular user to access and name it b.txt.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了