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?
CURLOPT_HEADERtofalse?enctype=”multipart/form-data”needs to be set for file transfers.CURLOPT_HEADERtotrue?CURLOPT_HEADERtoarray("Content-Type:multipart/form-data");and checkCURLOPT_HEADERorCURLOPT_HTTPHEADERandCURLOPT_HEADERto true? And thanks for your answer!