【发布时间】:2015-08-12 06:21:48
【问题描述】:
我的 php 网站托管在 Debain 机器中,我想将文件从该机器移动到另一个通过 VPN 连接的 Debain。
我试过 shell_exec 和 scp ,正如提到的 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