简体   繁体   中英

Curl File Upload Php

I am working on a project where I have to upload an image using cURL. My image name is not getting saved properly in the Database.

I am sending below line of code to my SDK,which has a function to get all the data and pass it to the route.

  $data = array(
        "appCode"    => $appCode,
        "logo" => new \CURLFILE($request->logo),
        "descriptionOfUse" => $request->descriptionOfUse,
        "descriptionOfFetch" => $request->descriptionOfFetch,
        "cancellationUrl" => $request->cancellationUrl ?? null,
        "helpPageUrl" => $request->helpPageUrl,
        "consentValidatityDays" => $request->consentValidatityDays ?? 0,

    );

SDK File

public function addConsentRule($data)
{
    $curl = curl_init();
    dd($data);
    curl_setopt_array($curl, array(
        CURLOPT_PORT           => $this->port,
        CURLOPT_URL            => $this->endPointUrl . "consentRule",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING       => "",
        CURLOPT_MAXREDIRS      => 10,
        CURLOPT_TIMEOUT        => 30,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST  => "POST",
        CURLOPT_POSTFIELDS     => $data,
        CURLOPT_HTTPHEADER     => array(
            "Accept: application/json",
            // 'Content-Type: multipart/form-data;'
        ),
    ));

    $response = curl_exec($curl);
    $err      = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        // return $response;
        return json_decode($response, true);
    }

}

When I dd the variable which I am passing to the SDK it misses the mime and postname and the response is below

array:7 [
 "appCode" => "2y105LSDDbI2ZML201a1VnrekOdIsA2SR06mWYhmIWZ3vsm4jE312S"
 "logo" => CURLFile {#317
 +name: "/tmp/phpUY9KmN"
 +mime: ""
 +postname: ""
}
 "descriptionOfUse" => "use"
 "descriptionOfFetch" => "fetch"
 "cancellationUrl" => "uri"
 "helpPageUrl" => "uri"
 "consentValidatityDays" => "45"
]

Found the solution,might be helpful to someone.

getRealPath() ,will get the path.

getMimeType() ,will get the image type

getClientOriginalName() ,will get the file name

  "logo" => new \CurlFile($request->logo->getRealPath(), $request->logo->getMimeType(), $request->logo->getClientOriginalName()),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM