Jump to content

Sending files through proxy


brccabral

Recommended Posts

Hi everyone!

 

The code below works fine with my internet connection.

But I have to use this code at my company, where the connections are via proxy.

How can I make php to get the permission to the proxy?

I've been told that I can use

proxy: name.of.proxy.com

port: 8080

and I have proxy's login and password

 

<?php
set_time_limit(600);
$server="xxx.xxx.xxx.xxx";
$user="username";
$pass="secret";
$port=22;

$filename="myfile.txt";

$sourcepath="C:/source/path/";
$sourcefile=$sourcepath.$filename;

$destpath="/dest/path/";
$destfile=$destpath.$filename;

try
{
    echo "Begin ".date('d/m/Y H:i')."\n";
    $sftp = new SFTPConnection($server, $port);
    $sftp->login($user, $pass);
    $sftp->uploadFile($sourcefile, $destfile);
    echo "End ".date('d/m/Y H:i')."\n";
}
catch (Exception $e)
{
    echo $e->getMessage() . " ".date('d/m/Y H:i')."\n";
}

class SFTPConnection
{
    private $connection;
    private $sftp;

    public function __construct($host, $port=22)
    {
    	echo "Connecting to $host on port $port.\n";
        $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)
    {
    	echo "Loggin $username\n";
        if (! @ssh2_auth_password($this->connection, $username, $password))
            throw new Exception("Could not authenticate with username $username " .
                                "and password $password.");

        echo "SFTP session\n";
        $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;
        echo "Creating $sftp$remote_file\n";
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');

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

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

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

        @fclose($stream);
    }
}

?> 

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/198048-sending-files-through-proxy/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.