There are lots of answers to "upload file with php curl", and they all require that a physical file exists on the machine. It's uploaded by referencing the @ symbol before the filename in the POST fields.
However, I have the file data already in a PHP variable: $data. How can I upload this directly without needing to first write it to a file?
I tried this:
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => SERVER_URL . '/test.php?abc=def&ghi=jkl',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 120,
CURLOPT_POST => 1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => ['file' => $data],
CURLOPT_HTTPHEADER => ["Content-type: multipart/form-data"],
CURLOPT_HEADER => true,
CURLOPT_INFILESIZE => strlen($data)
]);
$response = curl_exec($ch);
But my server gives an error that POST Content-Length exceeds the server limit of 8388608 bytes. However, in my php.ini file on the server, I have upload_max_filesize = 500M. So it looks like this POST is not being treated as a multipart file upload, but instead is trying to jam all the file data into the POST request.
Thanks for any advice.