1

I have small php app, that have some files inside. I want to send one of the files from one server to another server with using curl.

I already have file on server, I just need to send it to another server.

I execute the following code:

$url = "http://localhost:3919/";
$myCurl = curl_init();
curl_setopt($myCurl, CURLOPT_URL, $url);
curl_setopt_array($myCurl, array(
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POST => true,
                CURLOPT_HEADER => false,
                CURLOPT_SAFE_UPLOAD => true,
                // CURLOPT_HTTPHEADER => array(
                //  "Content-Type: multipart/form-data"),
                CURLOPT_POSTFIELDS => array(
                    "inputFile" => new CurlFile('@' . './Files/example.docx'))
            ));

$response = curl_exec($myCurl);
curl_close($myCurl);
return $response;

But it doesn't trigger the server. So what should I do wrong?

For example in other case (other app) I use this:

$file = $_FILES["inputFile"];
curl_setopt_array($myCurl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HEADER => false,
    CURLOPT_SAFE_UPLOAD => true,
    CURLOPT_HTTPHEADER => array(
        "Content-Type: multipart/form-data"),
    CURLOPT_POSTFIELDS => array(
        "inputFile" => new CurlFile($file["tmp_name"], $file["type"], $file["name"]))
));

And it works perfectly.

6
  • Have you checked curl_error()? Also, you say that you want to send the file to "another" server, but you're using a local url (with an odd port for a web server too). Commented Apr 18, 2018 at 17:31
  • It's my another app with localhost, where I get file. Yes, I've checked curl_error(), it's empty. Commented Apr 18, 2018 at 17:35
  • Okay, so then what exactly does "it doesn't trigger the server" mean? Is it supposed to return something? If so, provide the code that should be generating that response as well as what the current response is. Right now you're basically just saying "it's not working", which isn't helpful. Commented Apr 18, 2018 at 17:38
  • I just want to know why this code doesn't send file to server (server it's c# web api app where I set breakpoint to look at parameters that is sent from php file) Commented Apr 18, 2018 at 17:42
  • So far, you haven't provided anything to show that it's not sending the file. Also, I'm pretty sure that if you're using CurlFile, you don't use the @ prefix. CurlFile is a replacement for that method of sending files. You should see that in your working code you don't have that. Commented Apr 18, 2018 at 17:46

2 Answers 2

1

Using the curl_file_create works for me on sending files in curl.

$target_url = 'http://localhost:3919/';

$post = array (
    'file' => curl_file_create('Location of the file')
);

$ch = curl_init ($target_url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$msg=curl_exec ($ch);
$info = curl_getinfo($ch);

if($info['http_code'] === 200){
    $returnMessage = json_decode($msg,1);
} else {
    $returnMessage['file_type'] = 'error';
}
curl_close($ch);
Sign up to request clarification or add additional context in comments.

1 Comment

it also didn't help me.I see this in $info variable screencast.com/t/stsvNMVOUaEZ
0

maybe you need validate if exist files on your post and this a example complete:

<form enctype="multipart/form-data" encoding='multipart/form-data' method='post' action="form.php">
  <input name="uploadedfile" type="file" value="choose">
  <input type="submit" value="Upload">
</form>
<?
if ( isset($_FILES['uploadedfile']) ) {
 $filename  = $_FILES['uploadedfile']['tmp_name'];
 $handle    = fopen($filename, "r");
 $data      = fread($handle, filesize($filename));
 $POST_DATA = array(
   'file' => base64_encode($data)
 );
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, '<span style="color: red;">http://extserver.com/handle.php</span>');
 curl_setopt($curl, CURLOPT_TIMEOUT, 30);
 curl_setopt($curl, CURLOPT_POST, 1);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
 $response = curl_exec($curl);
 curl_close ($curl);
 echo "<h2>File Uploaded</h2>";
}
?>

this is a example with encrypt the file with base64.

your external server

$encoded_file = $_POST['file'];
$decoded_file = base64_decode($encoded_file);
/* Now you can copy the uploaded file to your server. */
file_put_contents('<span style="color: red;">subins</span>', $decoded_file);

3 Comments

No, I already have file on server, I just need to send it to another server.
on this case you need attachment the file
When I try your example, I see this screencast.com/t/WsQkD3BHa For file path I tried 'D:\\Share\\Programs\\xampp\\htdocs\\Files\\example.docx' and ./Files/example.docx and http://localhost/Files/example.docx. But I would like url independent path to file (not hardcoded).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.