PHP ftp connection to Amazon EC2 Instance - php

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

Related

PHP ftp_put fails with "Warning: ftp_put (): PORT command successful"

File is created on the FTP server, but its always 0 bytes large. Please give me a solution so that the file upload will working success.
I keep getting this warning:
Warning: ftp_put (): PORT command successful in C: \ xampp \ htdocs \ mailing \ teskirim-file-simpan2.php on line 30
FTP upload has failed!
My script is:
<?php
$ftp_server = "********";
$ftp_serverpath = "ftp.".$ftp_server;
$ftp_user_name = "********";
$ftp_user_pass = "***********";
$email_dir = "*******#*********";
$nyambungkeftp = ftp_connect($ftp_server);
if (false === $nyambungkeftp) {
throw new Exception('Unable to connect');
}
$loggedInnyambungkeftp =
ftp_login($nyambungkeftp, $ftp_user_name, $ftp_user_pass);
if (true === $loggedInnyambungkeftp) {
echo 'Success!';
} else {
throw new Exception('Unable to log in');
}
if ((!$nyambungkeftp) || (!$loggedInnyambungkeftp)) {
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";
}
// upload the file
$dest = 'detectip.txt';
$source = 'C:\xampp\htdocs\persuratan\file2\detectip.txt';
echo $dest;
echo $source;
$upload = ftp_put($nyambungkeftp, $dest, $source, FTP_ASCII);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($nyambungkeftp);
?>
PHP defaults to the active FTP mode. The active mode hardly ever works these days due to ubiquitous firewalls/NATs/proxies.
You almost always need to use the passive mode.
For that call the ftp_pasv after the ftp_login:
ftp_pasv($nyambungkeftp, true);
See my article on FTP connection modes, to understand, why you typically need to use the passive mode.
Try two things:
Try FTP_BINARY instead of FTP_ASCII
Try to use passive mode doc here

PHP script to upload a file to a FTP

I would like to a file MYFILE.csv to a remote FTP. Below is the script. The connection part works but not the file upload. I get the "There was a problem while uploading" message.
Thank you for your help.
<?php
$server = 'ftp.website.com' ;//Address of ftp server
$user_name = 'MYUSERNAME'; // Username
$password = 'MYPASSWORD'; // Password
$source_file = '/home/MYFILES.csv';
$dest = '/in/';
// set up basic connection
$connection = ftp_connect($server, 21) or die("Couldn't connect to $ftp_server");
echo "can connect";
echo "<br />";
// login with username and password
ftp_login($connection, $user_name, $password) or die("Cannot login");
echo "can login";
echo "<br />";
// upload a file
if (ftp_put($connection, $dest, $source_file, FTP_BINARY))
{ echo "successfully uploaded \n";}
else
{ echo "There was a problem while uploading \n";}
// close the connection
ftp_close($connection);
?>
Found the solution:
?php
$server = 'ftp.WEBSITE.com' ;//Address of ftp server
$user_name = 'MYUSERNAME'; // Username
$password = 'MYPASSWORD'; // Password
$source_file = '/home/MYFILE.csv';
$dest = '/in/MYFILE.csv';
// set up basic connection
$connection = ftp_connect($server, 21) or die("Couldn't connect to $ftp_server");
echo "can connect";
echo "<br />";
// login with username and password
ftp_login($connection, $user_name, $password) or die("Cannot login");
echo "can login";
echo "<br />";
// upload a file
ftp_put($connection, $dest, $source_file, FTP_ASCII) or die ("Cannot upload");
// close the connection
ftp_close($connection);
?>
i see the dest folder is "/in/".
Are you sure that its not trying to put it at the root folder of your ftp ?
(Which may belongs to root user, that's would be why it fail)

Copying files between two Debian Servers using php

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.
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.

Validate FTP credentials

I am writing a PHP page, which stores FTP Account's address, Username and Password.
I need to validate them against the server and tell user if the credentials provided are working.
It is possible to fire system commands, System commands are preferable so that a reusable script could be written.
So can anybody tell me how do I validate the ftp credentials on bash? I am CentOS.
You can use ftp_connect() and ftp_login() :
<?php
$conn = ftp_connect($ftp_server);
$result = ftp_login($conn, $ftp_user_name, $ftp_user_pass);
if ((!$conn) || (!result)) {
echo "Failed";
} else {
echo "Success";
}
ftp_close($conn);
<?php
function testFtpCredentials($server, $username, $password){
if(!is_string($server) or !strlen($server = trim($server))){
return null;
}
if(!is_string($username) or !strlen($username = trim($username))){
return null;
}
if(!is_string($password) or !strlen($password = trim($password))){
return null;
}
if(!$connection = ftp_connect($server)){
return false;
}
$result = ftp_login($connection, $username, $password);
ftp_close($connection);
return (bool)$result;
}
// How to use it.
var_dump(testFtpCredentials('ftp.server', 'username', 'password'));
?>
A function. Use it! Don't do system calls for such easy task.
You can use this code
try {
$con = ftp_connect($server);
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');
}
print_r(ftp_nlist($con, "."));
ftp_close($con);
} catch (Exception $e) {
echo "Failure: " . $e->getMessage();
}
PHP Has an FTP Module you can use to validate the credentials
Basic example from the docs
<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
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";
}
// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
// check upload status
if (!$upload) {
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream
ftp_close($conn_id);
?>
Of course, if you really want to FTP on the command line via exec() or something, there are resources like these docs which will help.
A working example I just tried:
file: ftp.sh
#! /bin/bash
USER=user#domain.com
PASS=xxxxxxxxxxxx
ftp -inv domain.com <<EOF
user $USER $PASS
ls -l
file: index.php
$result = shell_exec('sh ftp.sh');
var_dump($result);

File upload through FTP in php

I want to upload a file to multiple FTP.But it is showing error :
ftp_put() [function.ftp-put]: php_connect_nonb() failed: Operation now in progress (115).
Have a look to my code
foreach ($channel_details as $channel_list)
{
if(isset($connection))
unset($connection);
if(isset($login))
unset($login);
if(isset($upload))
unset($upload);
$server = $channel_list['channel'];
$ftp_user_name = $channel_list['username'];
$ftp_user_pass = $channel_list['password'];
$source='file_push.html';
$dest='/public_html/'.$path.$file;
$connection = ftp_connect($server) or die("Couldn't connect to ftp server");
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
// turn passive mode on
ftp_pasv($connection, true);
if (!$connection || !$login) { die('Connection attempt failed!'); }
$upload = ftp_put($connection, $dest, $source, FTP_ASCII);
if (!$upload) { echo 'FTP upload failed!'; }
ftp_close($connection);
}
Your code seems to be correct.
Maybe there is a firewall problem?
See here: https://bugs.php.net/bug.php?id=47110

Categories