How to copy / transfer files from one server to another?

Before 20 days I had one task, in which I need to copy / transfer image files from one server to another dynamically.

To fulfill my requirement I used php “libcurl” library, which created by Daniel Stenberg, and allows you to connect and communicate to many different types of servers with many different types of protocols.

Follow the under given steps to copy / transfer files from one server to another using curl :

Step : 1 : Copy and use the under given code in your php script

$ch = curl_init ("Source file’s complete url”);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata = curl_exec($ch);
// Check if any error occured 
if(curl_errno($ch)) 
{ 
  $fp = fopen(“Destination file’s complete url”, 'w');
  fwrite($fp, $rawdata);
  fclose($fp);
}
curl_close ($ch);
ob_flush();
flush();

Step : 2 : Replace

"Source file’s complete url”

properly.

Step : 3 : And replace

“Destination file’s complete url”

according to your requirement.

That’s it.

So now you are able to copy / transfer files from one server to another.

I hope this will help you….!!

Speak Your Mind

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: