Server to server file transfer using php
Moderator: General Moderators
Server to server file transfer using php
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
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
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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?
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?
Here is my code:this php file i kept in server1.
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
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);
?>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
Ah, I remember. Why didn't you continue on viewtopic.php?p=336035 ?
Quite annoying.
Quite annoying.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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.
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.
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
I tried by putting
Code: Select all
ini_set("max_execution_time",0);Then also file is transfering from server1 to server2 with 0 bytes.
Please suggest me what could be the problem......
Thanks in advance
MADHU
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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.
http://us2.php.net/manual/en/ref.info.p ... ution-time
If your server is running in safe mode you can't change it.
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?
Is your script running with error_reporting E_ALL and display_errors?
The remaining script does not work without this file -> requiremadhu wrote:include("inc.php"); //contains all database details of server1
I suggest using $db in the subsequent mysql function calls -> mysql_select_db(...,$db); mysql_query(...,$db) and so onmadhu wrote:$db = mysql_connect
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:$result = mysql_query("select * from mobile_online");
Why unlink() and 'a'?madhu wrote:unlink("madhu.csv"); //DELETING EXISTING FILE
$fp = fopen("madhu.csv", "a");
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.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.
returning from what?madhu wrote:if (ftp_put($conn_id, $remote_file, $source_file, FTP_ASCII)){
echo "successfully uploaded $source_file\n";
return true;
}
Now it's even returing before printing its text.madhu wrote:else{
return false;
echo "There was a problem while uploading $source_file\n";
}
ftp_close cannot be reached because of the return statements.madhu wrote:ftp_close($conn_id);
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";
}
?>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
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