• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

upload file to a different server

Hello chaps

Ok I need to on the fly

1/ download a file 'members.php' from my server
2/ then upload that file to a different server with the file name 'members.list'

Anyone know how I can do this?
0
submissiontechnology
Asked:
submissiontechnology
  • 8
  • 7
1 Solution
 
str_kaniCommented:
I assume that you have server A and B
A has members.php and B is the destination

<?php
// set up basic connection to server B
$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";
   }
$source_file=path_to_file."/members.php";
$destination_file="members.list"
// 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);
?>
I didn't test run the code. Hopefully will run with NO errors

Hope this helps.
0
 
submissiontechnologyAuthor Commented:
Thanks

Where do i enter the ftp info?
0
 
str_kaniCommented:
Fill these and add  at the begining...

$ftp_server=yourserver.com;
$ftp_user_name="username";
$ftp_user_pass="****";
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
submissiontechnologyAuthor Commented:
I get this error:

Parse error: parse error, unexpected T_VARIABLE in /home/virtual/site4/fst/var/www/html/admin/newsletter_ftp_upload.php on line 24
0
 
str_kaniCommented:
This line missing a ;

$destination_file="members.list";

0
 
submissiontechnologyAuthor Commented:
I get this now:

Warning: ftp_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/virtual/site4/fst/var/www/html/gpadmin/newsletter_ftp_upload.php on line 8

Warning: ftp_login() expects parameter 1 to be resource, boolean given in /home/virtual/site4/fst/var/www/html/admin/newsletter_ftp_upload.php on line 11
FTP connection has failed!Attempted to connect to ***** for user upload
0
 
submissiontechnologyAuthor Commented:
Right i fixed that but now i get this:

Connected to #####.co.uk, for user upload FTP upload has failed!
0
 
str_kaniCommented:
Have you specified the correct path for the destination ?

ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

can you give me a clue something like #####.co.uk ?
0
 
submissiontechnologyAuthor Commented:
This is what i have for: $destination_file="test.htm";
0
 
str_kaniCommented:
When you login to the ftp it will take you to the root folder then you have to change directory the target where you want to upload the file... put the following line as follows ...
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
------------------------------

echo "Current directory: " . ftp_pwd($conn_id) . "\n";
// try to change the directory to somedir
if (ftp_chdir($conn_id, "somedir")) {
   echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
   $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
} else
{
   echo "Couldn't change directory\n";
}

0
 
str_kaniCommented:
i.e try something like

<?php
// set up basic connection to server B
$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";
   }
$source_file=path_to_file."/members.php";
$destination_file="members.list";
echo "Current directory: " . ftp_pwd($conn_id) . "\n";

//change to the target directory
if (ftp_chdir($conn_id, "somedir")) {
   echo "Current directory is now: " . ftp_pwd($conn_id) . "\n";
   // upload the file
   $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
} else
{
   echo "Couldn't change directory\n";
}


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


?>
0
 
submissiontechnologyAuthor Commented:
I get this now:

Connected to ###.co.uk, for user uploadCurrent directory: / Current directory is now: /test FTP upload has failed!
0
 
str_kaniCommented:
add the folowing lines before ---> // check upload status

echo "destination=".$destination_file;
echo "<br>source=".$source_file;

and let me know what you get.
0
 
submissiontechnologyAuthor Commented:
I gets this:

Connected to ###.co.uk, for user uploaddestination=/test.htm
source=path_to_file/admin/index.phpFTP upload has failed!


/admin/index.php this file is in a httacces directory and needs a username and password would that stop it?
0
 
str_kaniCommented:
source is in server A say source.co.uk
source=path_to_file/admin/index.php
i.e like
source.co.uk/admin/index.php
and the above script example (copy_file.php ) is in the same server

Destination is server B say target.co.uk
uploaddestination=/test.htm

i.e target.co.uk/test.htm

this will copy file to the root of target server.  

>>> /admin/index.php this file is in a httacces directory and needs a username and password would that stop it?

more chances ..

Don't copy that file inside protected dir at the moment.

Put a test file in the public folder and try...






0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now