Skip to content

Commit a881637

Browse files
author
Ajdin Lokvancic (TME)
committed
Fix file upload process to support multiple file uploads using cURL
1 parent dae402c commit a881637

1 file changed

Lines changed: 49 additions & 15 deletions

File tree

docs/api/client/files.md

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -869,26 +869,60 @@ curl_close($ch);
869869
$signedUrl = $data['attributes']['url'];
870870

871871
// Step 2: Upload multiple files
872-
$ch = curl_init();
873-
curl_setopt($ch, CURLOPT_URL, $signedUrl);
874-
curl_setopt($ch, CURLOPT_POST, true);
875-
876-
// Build multipart form data with multiple files
877-
$postFields = ['directory' => $directory];
878-
foreach ($filePaths as $index => $filePath) {
879-
$postFields["files[{$index}]"] = new CURLFile($filePath);
872+
// Note: PHP's cURL doesn't support multiple values for the same field name
873+
// in the same way as other languages. Each file needs to be uploaded
874+
// separately or use a custom multipart body builder.
875+
876+
// Option 1: Upload files one by one
877+
foreach ($filePaths as $filePath) {
878+
$ch = curl_init();
879+
curl_setopt($ch, CURLOPT_URL, $signedUrl);
880+
curl_setopt($ch, CURLOPT_POST, true);
881+
curl_setopt($ch, CURLOPT_POSTFIELDS, [
882+
'files' => new CURLFile($filePath),
883+
'directory' => $directory
884+
]);
885+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
886+
887+
$uploadResponse = curl_exec($ch);
888+
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
889+
curl_close($ch);
890+
891+
if ($httpCode !== 200 && $httpCode !== 204) {
892+
echo "Failed to upload: " . basename($filePath) . "\\n";
893+
break;
894+
}
880895
}
881896

882-
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
883-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
897+
echo "Files uploaded successfully\\n";
884898

885-
$uploadResponse = curl_exec($ch);
886-
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
887-
curl_close($ch);
899+
// Option 2: Use Guzzle for better multipart support
900+
// composer require guzzlehttp/guzzle
901+
/*
902+
use GuzzleHttp\\Client;
903+
use GuzzleHttp\\Psr7;
888904

889-
if ($httpCode === 200 || $httpCode === 204) {
890-
echo "Multiple files uploaded successfully\\n";
905+
$client = new Client();
906+
907+
// Get signed URL (same as above)...
908+
909+
$multipart = [];
910+
foreach ($filePaths as $filePath) {
911+
$multipart[] = [
912+
'name' => 'files',
913+
'contents' => Psr7\\Utils::tryFopen($filePath, 'r'),
914+
'filename' => basename($filePath)
915+
];
891916
}
917+
$multipart[] = [
918+
'name' => 'directory',
919+
'contents' => $directory
920+
];
921+
922+
$response = $client->post($signedUrl, [
923+
'multipart' => $multipart
924+
]);
925+
*/
892926
?>`
893927
}}
894928
/>

0 commit comments

Comments
 (0)