【问题标题】:Copying files between two Debian Servers using php使用 php 在两个 Debian 服务器之间复制文件
【发布时间】:2015-08-12 06:21:48
【问题描述】:

我的 php 网站托管在 Debain 机器中,我想将文件从该机器移动到另一个通过 VPN 连接的 Debain。

我试过 shell_execscp ,正如提到的 here

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

我也尝试过使用 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() . "\n";
}

?>

只是我的问题是我无法将文件从我的 php 应用程序正在运行的一台机器移动到另一台通过 VPN 连接的机器。

【问题讨论】:

  • 什么不起作用?是否有任何(错误)日志?服务 (ssh/ftp) 是如何配置的?服务器甚至能够“看到”(ping)彼此吗?您是否尝试过直接在服务器上执行命令(例如 scp)而不使用 PHP/apache?
  • @SteffenWinkler 都可以互相 ping 通。是的,我执行了 scp -r /tempFolder/ root@xxx.xxx.xx.xxx:/usr/local/ 并且它有效。什么没有奏效?文件未传输。错误:空白页。我双重交叉,错误报告设置为显示错误

标签: php debian vpn file-transfer


【解决方案1】:

我安装了proftpd并创建了一个用户here

请注意默认端口为 21。并确保重启 proftpd:

service proftpd restart

我用来上传文件的代码是:

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\n";
} else {
 echo "There was a problem while uploading $file\n";
 die();
}
ftp_close($conn_id);
?>

这里 a.txt 存在于 index.php 的同一目录中。

它会将文件复制到您提到的该特定用户可以访问的文件夹中并将其命名为 b.txt。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多