Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zacky_84's avatar

cURL with File Upload Problem

How do I include the file path in cURL? However when I use command line curl, it works fine. Here's my code:

$token = "pAssWRODaCCessToKEN";
$dataJson = '{"businessServiceId":"742869", "docTypeId":"23", "folderId":"284493999", "documentStructure":"0"}';

$header = array(
    'Authorization: Bearer '.$token,
    'client_id: 123321
    'grant_type: jwt-bearer'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'file' => new \CurlFile('storage/test1.txt'),
    'data' => json_encode($dataJson).';type: application/json'
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, 'https://my.privateserver.com/api/v1/documents');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
$json_response = curl_exec($ch);
$result = json_decode($json_response, true);
return $json_response;
return $result;   

Here's the working cURL command: curl -v -H "Authorization:Bearer pAssWRODaCCessToKEN" -H "grant_type:jwt-bearer" -H "client_id: 123321" -F "file=@"/jet/app/www/default/my/storage/app/public/test1.txt"" -F "data={"bSId":"742869", "docTypeId":"23", "folderId":"288411499", "documentStructure":"0"};type=application/json" "https://my.privateserver.com/api/v1/documents"

0 likes
1 reply
jozekj's avatar

Have you ever found a resolution to this problem? I have found myself in the same situation. My Curl upload works from terminal but does not from within laravel.

All troubleshooting seems to point to the file path not being correct or similar. It doesn't seem to see the file in question, no matter on file location or path used.

$authData = array( 'key' => $apiKey, 'signature' => $signature, 'nonce' => $nonce++ ); $authString = http_build_query($authData); $header = 'Authorization: Splynx-EA (' . $authString . ')';

$ch = curl_init();

$file_name_with_full_path = new CurlFile('CA_Customer_Sign_Up_Details_ENC.pdf','application/pdf', 'CA_Customer_Sign_Up_Details_ENC.pdf');

$post = array( 'file' => $file_name_with_full_path, );

curl_setopt($ch, CURLOPT_URL, $domainName . "/api/2.0/admin/customers/customer-documents/$doc_id--upload"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_HTTPHEADER, array( $header, "Content-Type: multipart/form-data", ));

$response = curl_exec($ch); curl_close($ch);

// var_dump($response); print_r($response);

Please or to participate in this conversation.