Page 1 of 2
Server to server file transfer using php
Posted: Tue Dec 05, 2006 11:36 pm
by madhu
Hi all,
If any one knows how to transfer a file from one server to another , then please suggest me how to implement.
Waiting for your positive and valuable replies........
Thanks and regards
MADHU
Posted: Tue Dec 05, 2006 11:53 pm
by AKA Panama Jack
Posted: Tue Dec 05, 2006 11:55 pm
by volka
Not exactly sure what you mean.
Do you want to transfer a file from the server where ph is running to (any) other server (all options of how to transfer the data open)?
Do you want to use ftp and have (possibly) three computers involved: the source, the target and the controller without transfering the data to from source to controller first and then to target?
Posted: Wed Dec 06, 2006 12:11 am
by madhu
Here is my code:this php file i kept in server1.
Code: Select all
<?
//DATABASE CONNECTION FOR THE FIRST SERVER
include("inc.php"); //contains all database details of server1
$db = mysql_connect($my_server,$my_user,$my_pass) or die("Unable to connect to MySQL");
mysql_select_db($data_base) or die("failed opening database");
$result = mysql_query("select * from mobile_online");
//FETCH THE RECORDS AND WRITE TO CSV FILE
unlink("madhu.csv"); //DELETING EXISTING FILE
$fp = fopen("madhu.csv", "a");
fwrite($fp, "PAGEDATE, STATUS, EDITIONID, PUBLISHTYPE, PUBLISHBY\n");
while($row = mysql_fetch_array($result)){
fwrite($fp, "$row[page_date], $row[status], $row[edition_id], $row[publish_type], $row[publish_by]\n");
}
fclose($fp);
/*TILL HERE IT WILL CREATE A FILE madhu.csv IN SERVER1.*/
//CONNECT TO SERVER2
$ftp_server = "HOSTNAME";
$ftp_user_name = "UNAME";
$ftp_user_pass = "PASSWORD";
$source_file = 'madhu.csv'; //FROM SERVER1
$remote_file = 'madhu.csv'; //TO SERVER2
$conn_id = ftp_connect($ftp_server);
if($conn_id) echo "connection success....\n";
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if($login_result) echo "login success....\n";
if (ftp_put($conn_id, $remote_file, $source_file, FTP_ASCII)){
echo "successfully uploaded $source_file\n";
return true;
}
else{
return false;
echo "There was a problem while uploading $source_file\n";
}
ftp_close($conn_id);
?>
After executing this code from server1, in server1 it is creating a file madhu.csv and after that it is connecting to server2, login into the server2 and uploading the file madhu.csv to server2.
But it is uploading only the file and the content inside the file madhu.csv is not transfering to the server2.
Please suggest me what couldbe the problem........
Thanks in advance
MADHU
Posted: Wed Dec 06, 2006 12:38 am
by volka
Ah, I remember. Why didn't you continue on
viewtopic.php?p=336035 ?
Quite annoying.
Posted: Wed Dec 06, 2006 12:50 am
by madhu
Dear volka,
Am sorry to post this topic today seperately.
Please suggest me what couldbe the problem in my code.
Waiting for positive response...
MADHU
Posted: Wed Dec 06, 2006 11:04 am
by AKA Panama Jack
There could be many reasons why it isn't working. I gather you have tried running this yourself to see if any error messages come up.
It almost sounds like your PHP program is failing due to the execution time taking too long. You could add set_time_limit(600); at the start of the script to see what happens. Plus you need to add some more error reporting to track down the possible problem.
Posted: Wed Dec 06, 2006 10:47 pm
by madhu
Hi AKA Panama Jack,
I tried by putting
in the 1st line of my program.
Then also file is transfering from server1 to server2 with 0 bytes.
Please suggest me what could be the problem......
Thanks in advance
MADHU
Posted: Wed Dec 06, 2006 11:07 pm
by AKA Panama Jack
You need to read this...
http://us2.php.net/manual/en/ref.info.p ... ution-time
If your server is running in safe mode you can't change it.
Posted: Wed Dec 06, 2006 11:22 pm
by volka
If you connect to Server2 with a "normal" ftp client with the same credentials and upload a file does it say something about PASV or
passive mode?
Is your script running with error_reporting E_ALL and display_errors?
madhu wrote:include("inc.php"); //contains all database details of server1
The remaining script does not work without this file -> require
madhu wrote:$db = mysql_connect
I suggest using $db in the subsequent mysql function calls -> mysql_select_db(...,$db); mysql_query(...,$db) and so on
madhu wrote:$result = mysql_query("select * from mobile_online");
You do not test wether the query succeeded ($result!==false). Just to be on the safe side name all the fields you need instead of using *
madhu wrote:unlink("madhu.csv"); //DELETING EXISTING FILE
$fp = fopen("madhu.csv", "a");
Why unlink() and 'a'?
http://de2.php.net/fopen wrote:'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
You're testing for success in the ftp section of the script. But if something fails the remaining ftp functions are called nonetheless. Either check for success and only then executed the subsequent actions or test for failure and then abort.
madhu wrote:if (ftp_put($conn_id, $remote_file, $source_file, FTP_ASCII)){
echo "successfully uploaded $source_file\n";
return true;
}
returning from what?
madhu wrote:else{
return false;
echo "There was a problem while uploading $source_file\n";
}
Now it's even returing before printing its text.
madhu wrote:ftp_close($conn_id);
ftp_close cannot be reached because of the return statements.
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
require 'inc.php';
$ftp_server = "HOSTNAME";
$ftp_user_name = "UNAME";
$ftp_user_pass = "PASSWORD";
$source_file = 'madhu.csv';
$remote_file = 'madhu.csv';
$db = mysql_connect($my_server,$my_user,$my_pass) or die(mysql_error());
mysql_select_db($data_base, $db) or die(mysql_error());
$query = 'select
`page_date`, `status`, `edition_id`, `publish_type`, `publish_by`
from
mobile_online';
$result = mysql_query($query, $db) or die(mysql_error());
$fp = fopen($source_file, 'w');
$bytesWritten = fwrite($fp, "PAGEDATE, STATUS, EDITIONID, PUBLISHTYPE, PUBLISHBY\n");
while($row = mysql_fetch_array($result)) {
$bytesWritten += fwrite($fp, "$row[page_date], $row[status], $row[edition_id], $row[publish_type], $row[publish_by]\n");
}
fclose($fp);
echo '<div>written: ', $bytesWritten, '. Filesize: ', filesize($source_file), "</div>\n"; flush();
echo "<div>connecting ftp</div>\n"; flush();
$conn_id = ftp_connect($ftp_server) or die('cannot connect ftp server');
echo "<div>ftp login</div>\n"; flush();
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die('ftp login failed');
echo "<div>ftp put</div>\n"; flush();
if (ftp_put($conn_id, $remote_file, $source_file, FTP_ASCII)) {
echo "successfully uploaded $source_file\n";
}
else{
echo "There was a problem while uploading $source_file\n";
}
echo "<div>ftp_close</div>\n"; flush();
if ( ftp_close($conn_id) ) {
echo "Done.\n";
}
else {
echo "ftp_close failed.\n";
}
?>
Posted: Thu Dec 07, 2006 12:34 am
by madhu
Hi volka,
i tried by executing, then it gave the following error:
written: 21731. Filesize: 21731
connecting ftp
ftp login
ftp put
Warning: ftp_put() [function.ftp-put]: Failed to establish connection. in /var/www/vhosts/bs-epaper.com/httpdocs/bsepaper/transfer_file.php on line 32
There was a problem while uploading madhu.csv
ftp_close
Done.
Please suggest me what couldbe the problem...
Waiting for your valuable replies.......
Regards
MADHU
Posted: Thu Dec 07, 2006 12:35 am
by volka
volka wrote:If you connect to Server2 with a "normal" ftp client with the same credentials and upload a file does it say something about PASV or passive mode?
Posted: Thu Dec 07, 2006 12:46 am
by madhu
"normal" ftp client means , from Local machine to server2 ?????
I tried to upload from my Local machine to Server2 directly, then it is uploading successfully...
But from server1 to server2, it is failing...
Posted: Thu Dec 07, 2006 12:58 am
by madhu
Hi volka,
Finally i got the solution.
Before uploading i.e before usinf ftp_put(), i placed
ftp_pasv($conn_id, true);
Then it is uploading successfully to the second server.
Any way thank you somuch for your valuable replies....
Regards
MADHU
Posted: Thu Dec 07, 2006 2:54 am
by volka
madhu wrote:Before uploading i.e before usinf ftp_put(), i placed
ftp_pasv($conn_id, true);
That's why I wanted to know wether your ftp client tells you anything about PASV or passive mode.