1

I'm trying to send a .txt file via php cURL post but no success.

My cURL code looks like this:

$filedata = curl_file_create('../path/to/file.txt', 'text/plain', 'file.txt');

$array = array(
    'action' => 1,
    'file' => $filedata,
    'sec_secure' => $sec_secure,
)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain_name.com/url/to/script.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);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$result = curl_exec($ch);
curl_close($ch);

$response = 0;

if($sec_secure == $result){
    $response = 1;
}

And the receiving script.php:

if(isset($_FILES['file'])){
    $contents = file_get_contents($_FILES['file']['tmp_name']);
    $response = $_POST['sec_secure'];
}
else{
    $response = '';
}
echo $response;

The problem is that is always giving me $response = 0.

Does anyone knows why is this happening and how to make it work as it would?

EDIT:

I missed this following line after $array:

$array = http_build_query($array);

Maybe that's causing the issue?

7
  • why have you set CURLOPT_HEADER to false ? Commented Jan 16, 2018 at 8:44
  • also enctype=”multipart/form-data” needs to be set for file transfers. Commented Jan 16, 2018 at 8:45
  • @RamC could you tell me how to set the enctype? And do I need to set CURLOPT_HEADER to true? Commented Jan 16, 2018 at 8:48
  • set CURLOPT_HEADER to array("Content-Type:multipart/form-data"); and check Commented Jan 16, 2018 at 8:52
  • @RamC on CURLOPT_HEADER or CURLOPT_HTTPHEADER and CURLOPT_HEADER to true? And thanks for your answer! Commented Jan 16, 2018 at 8:54

2 Answers 2

1

You have to set the enctype=”multipart/form-data” for file transfers.

set CURLOPT_HEADER to true

set CURLOPT_HTTPHEADER to array("Content-Type:multipart/form-data");

Sign up to request clarification or add additional context in comments.

10 Comments

The following works perfectly without headers: 3v4l.org/E0cHv, try it... it will send the file then print_r the file contents. The OP is essentially doing the same but with 1 instead of the file contents so I dont think its headers.
Thanks! I'll try it later when I get home
I just tried your code and still returns value 0. I don't know how to fix this...
The code is what I have written earlier. I'm starting to think that maybe I don't have to retrieve it as $_FILES but as $_POST. And also I'm not getting any errors on errorlog [both]... And again, thanks for helping me out :)
I've just edit my code cause I missed one line in the first place but don't know if that's relevant...
|
0

Add Header in curl.

$array = array(
    'action' => 1,
    'file' => $filedata,
    'sec_secure' => $sec_secure,
)
$array = json_encode($array);

$headers = array(

        'Content-Type: application/json',
            'Content-Length: ' . strlen($array)
    );

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $array);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$result = curl_exec($ch);
curl_close($ch);

$response = 0;

if($sec_secure == $result){
    $response = 1;
}

change the code like this.

1 Comment

Thanks! I'll try it later when I get home!

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.