2

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() . "\n";
}

?>

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.

Community
  • 1
  • 1
  • what didn't work? Are there any (error) logs? How are the services (ssh/ftp) configured? Are the servers even able to 'see' (ping) eachother? Did you try executing the commands (scp for example) directly on the server without PHP/apache in between? – Steffen Winkler May 29 '15 at 11:51
  • @SteffenWinkler both can ping each other successfully. Yes I executed scp -r /tempFolder/ root@xxx.xxx.xx.xxx:/usr/local/ and it worked. What didn't worked? Files not transfered. Error: A blank page. I double crossed that error reporting is set to display error – Mishra Shreyanshu May 29 '15 at 12:00

1 Answers1

2

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\n";
} else {
 echo "There was a problem while uploading $file\n";
 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.