简体   繁体   中英

PHP ftp connection to Amazon EC2 Instance

Hi I have been struggling on this for one day. My ftp connection through putty is working file where i am passing public DNS and then upload .pem key for password. But when i am trying to do so through PHP it is not able to connect. Any help would be highly appreciated. My PHP Code is:

$server='AMAZON EC2 Public DNS';
$username='root';
$password='**i copy pasted key from .pem file**';

try {

    $con = ftp_connect($server);
    ftp_pasv($con, true);
    if (false === $con) {
        throw new Exception('Unable to connect');
    }

    $loggedIn = ftp_login($con,  $username,  $password);
    if (true === $loggedIn) {
        echo 'Success!';
    } else {
        throw new Exception('Unable to log in');
    }


    ftp_close($con);
} catch (Exception $e) {
    echo "Failure: " . $e->getMessage();
}
<?php
$ftp_server = 'your_amazon_instance_url';
$ftp_user_name = 'username';
$ftp_user_pass = 'password for ftp instance for user';
$conn_id = ftp_connect($ftp_server); 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
} else {
    echo "Connected to $ftp_server, for user $ftp_user_name <br/>";
}

Thanks everyone for your effort. I had solved this using SSH Connection to Amazon Ec2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM