I have 2 servers. And i want to transfer a file from one server to an other with cURL. Can anyone show me a good example of this ? What options should I give to cURL .....
Thx.
I have 2 servers. And i want to transfer a file from one server to an other with cURL. Can anyone show me a good example of this ? What options should I give to cURL .....
Thx.
Using cURL seems like using the wrong tool for the job(ie when you have a hammer every problem looks like a nail) based on what you have given, why not look at SCP/SFTP or even rsync if the former is not flexible enough. If not ,you would have to host the file on your source server and initiate cURL to request the file(ie over http)
Plenty of resources available, here are a couple:
http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
http://www.phpclasses.org/package/3753-PHP-Upload-files-via-HTTP-POST-using-Curl.html
Never used them nor had a need to. But should help you get started.
sender.php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// true to return the transfer as a string of the return value
// of 'curl_exec' instead of outputting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/receiver.php');
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
'euro' => '@eurodance.pls',
'flush' => '@flush_next.png',
'first_name' => 'Vadim'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
print_r($response);
receiver.php
if(isset($_FILES)){
$temp_file_name = $_FILES['euro']['tmp_name'];
$original_file_name = $_FILES['euro']['name'];
// Find file extention
$ext = explode ('.', $original_file_name);
$ext = $ext [count ($ext) - 1];
// Remove the extention from the original file name
$file_name = str_replace ($ext, '', $original_file_name);
$new_name = '_'.$file_name . $ext;
//echo $file_name ." ". $ext;
if (move_uploaded_file ($temp_file_name, $new_name)) {
echo "success";
} else {
echo "error";
}
}
if(isset($_FILES)){
$temp_file_name = $_FILES['flush']['tmp_name'];
$original_file_name = $_FILES['flush']['name'];
// Find file extention
$ext = explode ('.', $original_file_name);
$ext = $ext [count ($ext) - 1];
// Remove the extention from the original file name
$file_name = str_replace ($ext, '', $original_file_name);
$new_name = '_'.$file_name . $ext;
//echo $file_name ." ". $ext;
if (move_uploaded_file ($temp_file_name, $new_name)) {
echo "success";
} else {
echo "error";
}
}