-1

I'm trying to send data via cURL post but I've never tried it before and I don't know if I'm doing it right.

What I want to do is sent a file via post to a file from my remote server and there read the file and insert data into database but unfortunately it's not working and error_log doesn't show me anything.

My code looks like this:

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,"https://".$host."/file.php");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'file' => '@'.realpath(../path/to/file/'.$_POST['file_name'].'.txt'),
            'action' => 'first',
            'check' => $_POST['file_name'],
        ));
        $result = curl_exec($ch);
        curl_close($ch);

This code is placed after some sql querys and code made to write this sql results into the file that I'm trying to send.

molinet
  • 266
  • 3
  • 14
  • What do your receive from the server? Just print the `$result` variable. Also, you can read the HTTP status code `$responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);` before you close the connection. Or try without uploading the file – Ruben Jan 05 '18 at 08:10
  • 1
    An issue occurs because CURLOPT_POSTFIELDS is supposed to be http_build_query($array), not an array. http://php.net/manual/ro/function.http-build-query.php – Cat Jan 05 '18 at 08:12
  • @Ruben it's the first time I'm using cURL and I don't understand how it really works. What my code is doing is putting that information via post in the CURLOPT_URL, right? Or is it getting that information via post from that url? Cause I'm understanding it like the first case. – molinet Jan 05 '18 at 08:13
  • Is there a missing quote in `.realpath(../path/to/file/'.$_POST['file_name'].'.txt')` - should there be one before `../path` – Nigel Ren Jan 05 '18 at 08:18
  • @NigelRen you're right. Thanks for pointing that out! – molinet Jan 05 '18 at 08:23
  • @Cat I didn't know that. Thanks for the info! I'll try it later. – molinet Jan 05 '18 at 08:23

1 Answers1

-1

Here is the Answer by Shakir Khan you Should follow: PHP: upload file from one server to another server - Stack Overflow

OR you should read PHP Documentation carefully there is a huge amount of CONSTANT availabe to use: curl_setopt

Riajul
  • 1,140
  • 15
  • 20