如何使用我的ec2键scp与PHP

I use a command like this on my Mac to scp to my remote server (in a .sh file—it must be done automatically).

scp -i my_key.pem index.html ubuntu@<ip>:~/public/index.html;

Now I have to do the same thing from a Windows machine and figured I would just write it in php instead of using bash so that the code is the same on Windows and Mac (our app runs a local server too that already uses php scripts, it's complicated). I've looked at a few examples but can't find my exact solution. Also, to be honest I don't know much about private/public keys and I want to be careful.

I've got something like this. I don't know what to do for the key.

$hostname = '21.232.foo.bar'; 
$sourceFile = 'index.html';
$targetFile = '~/public/indexx.html';

// SSH Key File — I'm guessing this should be private, not public
    private $ssh_auth_priv = '~/.ec2/my_key.pem'; 

$connection = ssh2_connect($hostname, 22);

ssh2_scp_send($connection, $sourceFile, $targetFile, 0777);

Also, if I'm trying to do something stupid and there is a much easier way, please let me know. Thanks for your help.

~~ UPDATE

My code now looks as follows and I'm getting an error Warning: ssh2_scp_send(): Failure creating remote file: failed to send file

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php 

$hostname = '13.124.foo.bar'; 
$sourceFile = 'foo.txt';
$targetFile = '~/public2/foo.txt';

$connection = ssh2_connect($hostname, 22);

ssh2_auth_pubkey_file(
    $connection,
    'ubuntu',
    '~/.ec2/id_rsa.pub',
    '~/.ec2/bh.pem'
);


ssh2_scp_send($connection, $sourceFile, $targetFile, 0644);



 ?> 
 </body>
</html>

That's being hosted on my localhost:8000

The general format will be

1   <?php
2   $conn = ssh2_connect('example.com', 22);
3   ssh2_auth_pubkey_file(
4       $conn,
5       'username',
6       '/home/username/.ssh/id_rsa.pub',
7       '/home/username/.ssh/id_rsa'
8   );
9   ......

Use ssh2_scp_send($conn, '/local/filename', '/remote/filename', 0644); to send the file where 0644 are file permissions.

Check out http://www.patcup.com/php-ssh-authentication-using-a-public-key/ if you want to continue using ssh2 for this type of thing, but it's recommended you use https://github.com/phpseclib/phpseclib.