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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵