0

I am having a php enabled server1. I have a php code for file upload. But i need the file to be saved on server2.

I have FTP access to server2.

While searching i found this code,

    <?php
$ftp_server = "199.53.23.1";
$ftp_user_name = "xxxx";
$ftp_user_pass = "**********";
$remote_dir = "http://server2/Images/";

// 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);

//default values
$file_url = "";

if($login_result) {
//set passive mode enabled
ftp_pasv($conn_id, true);

$file = $_FILES["uploadedfile"]["tmp_name"];
$remote_file = $_FILES["uploadedfile"]["name"];

$ret = ftp_nb_put($conn_id, $remote_file, $file, FTP_BINARY, FTP_AUTORESUME);
while(FTP_MOREDATA == $ret) {
$ret = ftp_nb_continue($conn_id);
}

if($ret == FTP_FINISHED) {
echo "File '" . $remote_file . "' uploaded successfully.";
} else {
echo "Failed uploading file '" . $remote_file . "'.";
}
} else {
echo "Cannot connect to FTP server at " . $ftp_server;
}
?>

It said unable to connect to Server.

Anyone have an idea on this type of requirement?

Please help.

UPDATE

Server2 doesn't support PHP

1
  • First, go to 1st server and try to connect server 2 through FTP on command line. There may no ftp connection Commented Mar 24, 2014 at 12:14

1 Answer 1

2

Here your line

$login_result = @ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

Remove @

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

For SFTP

$conn_id = ssh2_connect($ftp_server, 22);
ssh2_auth_password($conn_id, $ftp_user_name, $ftp_user_pass);

$sftp = ssh2_sftp($conn_id);

Uses example: $stream = fopen("ssh2.sftp://$sftp/path/to/file", 'r');

*send a file

ssh2_scp_send($conn_id, '/local/filename', '/remote/filename', 0644);

*fetch file

ssh2_scp_recv($conn_id, '/remote/filename', '/local/filename');

*Create a new folder

ssh2_sftp_mkdir($sftp, '/home/username/newdir');

*Rename the folder

ssh2_sftp_rename($sftp, '/home/username/newdir', '/home/username/newnamedir');

*Remove the new folder

ssh2_sftp_rmdir($sftp, '/home/username/newnamedir');

*Create a symbolic link

ssh2_sftp_symlink($sftp, '/home/username/myfile', '/var/www/myfile');

*Remove a file

ssh2_sftp_unlink($sftp, '/home/username/myfile');
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, But Same problem. It says "Cannot connect to FTP server at IP address"
have you created the ftp server on 2nd PC? You must have to have that too! ftp_connect(host,port,timeout); port is normally 21
2nd PC? Sorry i dint get you?
Sorry, I meant server 2!
Yes, i have the ftp setup with SFTP and port 22
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.