Unfortunately, I don't have access to the filepath - I only have access to the filehandle (e.g. the result of fopen(path)
) as I'm working in Moodle.
Now, I want to send this filehandle over HTTP to a Node.js app for some processing. This is my PHP code right now:
foreach ($files as $file) {
//Only send files
if ($file->get_mimetype()) {
$fh = $file->get_content_file_handle();
$postdata[] = [
'name' => 'files[]',
'contents' => $fh,
'filename' => $file->get_filename(),
'headers' => [
'Content-Type' => $file->get_mimetype(),
],
];
}
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, '...');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: multipart/form-data',
]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
I'm new to PHP, so there are probably many reasons why this doesn't work - however, some guidance would be nice.