Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Cross Server File Transfer


[SOLVED] Cross Server File Transfer

Reply
  #1 (permalink)  
Old 03-10-09, 09:23 AM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
[SOLVED] Cross Server File Transfer

I need to transfer files between a network of servers all over the world. I have looked into using cURL and ftp_fput() but am having little luck. I can get a transfer going from my local to the server or visa versa but not from server to server.

Basically im trying to replicate Akamai on a very small scale. This is the scenario. I am working from home. I search for a file on Web Server 1 in Oregon. The file doesn't exist. I search my other servers and find that it is located on Server 2 in Germany. I then need to COPY the file from Web Server 2 to Web Server 1. That's it.

I have it all in place except getting the server-to-server transfer to work. Who can help? Thanks much.
Reply With Quote
  #2 (permalink)  
Old 03-10-09, 09:31 AM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
One other thing. The connections need to be made via FTP. I have IP, USER & PASSWORDS for each account. Thanks.
Reply With Quote
  #3 (permalink)  
Old 03-10-09, 10:15 AM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
cURL works!

One of the nice things about it is you can have multiple channels running at once.

If the files are small (because of potential memory limits) file_get_contents() works also.

In addition, fopen() & [fsockopen() {much complicated and a lot of effort }] will work.

I'd assume you'd need to setup some type of cron job running on the servers with a
list of files to be replicated, but without knowing more of your needs, it's hard to give
advice.

Also, you can look into rsync for server to server updating for the servers you have control over.

If you're trying to replicate actual web pages, then that's a different story and a lot
more complicated. You'd have to parse the files and initiate other "file get" operations
based on links discovered in the files - just like a browser does.

As with most things, there are a whole slew of ways to get information over the web with PHP.

And finally, if this is something you actually want to build yourself, you'll need to write
a client/server/switchover messaging system if you can't find something to just do it;
which I'm sure you can.

Google: "server to server file transfer" and a lot of links will come up.

CAVEAT: There are a number of sites purporting to offer PHP and other open source
SST apps, but some of the sites are phishing with trojans in waiting.

And finally, if you are trying to do this yourself, there's always the ever present need
for: "Error detection, recovery and mitigation." Just what you wanted hear, eh?
Reply With Quote
  #4 (permalink)  
Old 03-10-09, 10:20 AM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
Well that makes a big difference!

Then it's pretty much cURL, fsockopen() or fopen.
Reply With Quote
  #5 (permalink)  
Old 03-10-09, 02:47 PM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
Thanks for all your input. Let me explain a little more about what i have done. First of, each of the files I'm transferring are over 1Gb so file_get_contents() wont work. Files are not transferred until requested by the user. I would prefer to do it all in CURL, but as i said im having issues.

I have looked into SST scripts but none seems to satisfy.

My thinking is that cURL cannot directly transfer a file server to server (please correct if wrong). I thought that the way to make it work would be to call a file on Web Server 2 using cURL and add some parameters (file name to transfer) to the call so that when ran it would using cURL transfer the file to Web Server 1. Sound good? Problem, Web Server 2 doesn't have a URL that will resolve to a specific file on the server. Therefor again only cURL via FTP login can be used (as i see it).

So do further define my question, with cURL, is there a way to transfer files between servers directly using a curl statement like
Code:
$ch = curl_init("ftp://theuser:thepass@72.47.251.180/testfile.m4v");
but one that works more like below with a location to get and set from/to. I know the following code is incorrect, i just would like to know how to get it right.
Code:
$c = curl_init("ftp://theuser:thepass@72.47.251.180/testfile.m4v","ftp://theuser:thepass@42.76.151.236/testfile.m4v");
I know i have left out parts of the cURL process. curl_exec() and curl_close() are pretty standard. Thanks.
Reply With Quote
  #6 (permalink)  
Old 03-10-09, 11:21 PM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
This is a meaty problem!!!

Do have any control over any of the processes on Web Server 2?

From the sound of it, you don't, but correct me if I'm wrong.

Yes, the syntax you're showing is wrong.

You have to specify the file of interest and/or send commands (set options) to
communicate with the server on the other end. You then do something with
the file bytes received on your (Web Server 1) end (i.e. manage storage).

I know I could get this to work, but I've haven't had to do this at this level with cURL.

I had to solve a similar problem (file size), but I had control of all of the servers.

It's going to take awhile to get it to work.

I'll play with it in my spare time, but I'm sure you'll get it before that.

Maybe we'll get lucky and someone who's already got a solution will pipe in.

I'll keep searching and see what I come up with.

Here are a bunch of links I pulled from the net:

Search I used to grab most of these links:
http://www.google.com/search?hl=en&c...-revision&cd=1

Obviously you've been here:
http://curl.haxx.se/

http://phpsense.com/php/php-curl-functions.html

http://www.onlamp.com/pub/a/bsd/2002...n_openbsd.html
http://www.securityfocus.com/bid/33962
http://codesnippets.joyent.com/posts/show/1092
http://codesnippets.joyent.com/posts/show/1758
http://www.entropy.ch/blog/Mac+OS+X/...Completed.html
http://forums.macosxhints.com/archiv...hp/t-1006.html
http://neowiki.neooffice.org/index.p...load_NeoOffice
http://www.webdigity.com/index.php?action=tutorial%3Bcode=45
http://davidwalsh.name/download-file-using-cygwin-curl
http://www.ehow.com/how_2036713_use-...ttributes.html

This file is a VB project, but there may be some useful info in here:
http://www.planet-source-code.com/vb...7&lngWId=1#zip

http://script.wareseeker.com/PHP/dow...curl.zip/20035
Reply With Quote
  #7 (permalink)  
Old 03-11-09, 10:18 AM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
SOLVED -

Thanks for everyones help. I am posting my full function for those who may find it useful. To recap, this function "try's" to simulate Akamai's service.

1. GeoLoc user.
2. GeoLoc all servers
3. Calculate distance between user and each server.
4. Sort by distance.
5. Check to see if file is at closest server. If so download.
6. If file not at closest server, copy file from mother server to server missing file. Then notify user via email with download link to closest server.

Code:
function sst() {

	global $_GET, $_SERVER;

	/*   start geo loc   */
	// http://www.imaginerc.com/software/GeoCalc/
	require_once('standards/classes/geoplugin.class.php'); 
	$geoplugin = new geoPlugin();
	// http://www.phpclasses.org/browse/file/25516.html
	include_once("standards/classes/GeoCalc.class.php"); 
	$oGC = new GeoCalc();

	/*   set server ip's and FTP logins		*/
	$servers = array($_SERVER['REMOTE_ADDR'],'xxx.xx.xx.xxx','xxx.xx.xx.xxx');
	$user = array('','usr1','usr2');
	$pass = array('','pass1','pass2',);

	/*   get user location   */
	$geo[0]['ip'] = $servers[0];
	$geoplugin->locate($geo[0]['ip']);
	$geo[0]['lat'] = $geoplugin->latitude;
	$geo[0]['lon'] = $geoplugin->longitude;
	$geo[0]['city'] = $geoplugin->city;
	$geo[0]['countryName'] = $geoplugin->countryName;

	/*   get  servers data  */
	for ($i = 1; $i < count($servers); $i++) {

		$geo[$i]['ip'] = $servers[$i];
		$geo[$i]['user'] = $user[$i];
		$geo[$i]['pass'] = $pass[$i];
		$geoplugin->locate($geo[$i]['ip']);
		$geo[$i]['lat'] = $geoplugin->latitude;
		$geo[$i]['lon'] = $geoplugin->longitude;
		$geo[$i]['city'] = $geoplugin->city;
		$geo[$i]['countryName'] = $geoplugin->countryName;
		$geo[$i]['dist'] = $oGC->GCDistance($geo[0]['lat'],$geo[0]['lon'],$geo[$i]['lat'],$geo[$i]['lon']);

	}

	/*   order by distance.   */
	$geo = orderBy($geo, 'dist');

	/*   connect to closeest server   */
	$conn_id = ftp_connect($geo[1]['ip']) or die("Couldn't connect to $ftp_server");
	ftp_login($conn_id, $geo[1]['user'], $geo[1]['pass']);
	$contents = ftp_nlist($conn_id, "tts/incoming");
	ftp_close($conn_id);

	$localfile = '/user/dac15/tts/incoming/';
	$remotefile = '/user/'.$geo[1]['user'].'/tts/incoming/';

	/*  if file is on the closest server.    */
	if (find($_GET['name'],$contents)) {

		// http://www.awesomephp.com/?Tutorials*16/Download-file-with-resume,-stream-and-speed-options.html
		downloadFile($remotefile,$_GET['name'],900,false); 

	}else{ 
		
		/*   push file to the closest server requesting server.*/
		$ch = curl_init();
		$fp = fopen($localfile.$_GET['name'], 'r');
		curl_setopt($ch, CURLOPT_URL, 'ftp://'.$geo[1]['user'].':'.$geo[1]['pass'].'@'.$geo[1]['ip'].'/'.$remotefile.$_GET['name']);
		curl_setopt($ch, CURLOPT_UPLOAD, 1);
		curl_setopt($ch, CURLOPT_INFILE, $fp);
		curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile.$_GET['name']));
		curl_exec($ch);
		$error_no = curl_errno($ch);
		curl_close($ch);
		if ($error_no == 0) {
			$error = 'File uploaded succesfully.';

			$mailto[0][0] = $_SERVER['HTTP_EMAIL'];
			$mailto[0][1] = $_SERVER['HTTP_FNAME'];
			$mailto[0][2] = $_SERVER['HTTP_LNAME'];

			$content="
			Dear ".$mailto[0][1]."
			<br>
			Your file is ready for download. Go to this link in your preferred web browser.
			";
			
			// http://www.xpertmailer.com/
			if (smtp_mail($mailto,"DoD File Download",$content)) { Return 1; }

		}else{
			$error = 'File upload error.';
			echo $error;
		}

	}

	die;

}

function orderBy($data, $field, $reverse_sort='') {
	$code = "return strnatcmp(\$a['$field'], \$b['$field']);";
	usort($data, create_function('$a,$b', $code));
	if ($reverse_sort==1) { krsort($data); }
	return $data;
}
Reply With Quote
  #8 (permalink)  
Old 03-11-09, 10:48 AM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
Good Job! I'll have to give you a "Thank You!" :-)

[QUOTE=cesarcesar;170181]SOLVED -

Thanks for everyones help. I am posting my full function for those who may find it useful. To recap, this function "try's" to simulate Akamai's service.

1. GeoLoc user.
2. GeoLoc all servers
3. Calculate distance between user and each server.
4. Sort by distance.
5. Check to see if file is at closest server. If so download.
6. If file not at closest server, copy file from mother server to server missing file. Then
Reply With Quote
  #9 (permalink)  
Old 03-11-09, 02:49 PM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
I have another question to go along with this post. Once the cURL transfer is started on my 1GB file the screen just sits white while doing its thing (page load bar crawls). Is there a way to finish loading the page while the transfer is in process. This way the user doesn't have to wait for the transfer to finish, they are just there to make it happen. (they are not downloading at the time so it would be okay for them to continue). Thanks.
Reply With Quote
  #10 (permalink)  
Old 03-17-09, 04:37 AM
cesarcesar cesarcesar is offline
Newbie Coder
 
Join Date: Mar 2008
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
Code:
/**
 * Takes a needle and haystack (just like in_array()) and does a wildcard search on it's values.
 *
 * @param    string        $string        Needle to find
 * @param    array        $array        Haystack to look through
 * @result    array                    Returns the elements that the $string was found in
 */
function find ($string, $array = array ())
{
	foreach ($array as $key => $value) {
		unset ($array[$key]);
		if (strpos($value, $string) !== false) {
			$array[$key] = $value;
		}
	}
	return $array;
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Has anybody used SourceAnyWhere,could you help me out? xutaokelly Windows .NET Programming 0 08-22-05 05:41 AM
Upload .xls file from client's machine to SQL Server table bubberz ASP.NET 0 08-09-05 02:40 PM
Force Download of a file on a different server. wiseman7687 PHP 9 07-26-05 01:37 AM
Server open file altafingar ASP.NET 1 10-20-04 12:12 AM


All times are GMT -5. The time now is 08:30 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.