Error #26 when uploading file via Curl With PHP

Asked

Viewed 223 times

0

Hello,

First excuse if something is missing or if the question is not well formatted, my first question.

Well, I need to send a CSV file via CURL to an API that performs a registration, but when sending the file returns me the error 26 of the Curl (Curl failed with error #26), in which points read error.

First I checked the functioning of the API by POSTMAN and it is working correctly.

I have a function where you upload the file to the directory "./src/temp/", soon after the upload comes the function in which I perform a POST via Curl with the file sending.

Because of the doubts I performed a visual temporary check before the execution to check if the file exists and can read the file, but even returning me that it exists and can read the file, it still returns me the Curl error.

Follows code:

$file = "./src/temp/{$_FILES['arquive']['name']}";

if(file_exists($file)){
    echo "exite";
    if(fopen($file, "r")){
        echo " e conseguiu ler";
    }else{
        echo " e falhou ao ler";
    }
}else{
    echo "não existe";
}

try {
    $ch = curl_init($url);

    if ($ch === false) {
        throw new Exception('failed to initialize');
    }

    $cfile = new CURLFile($file , $_FILES['arquive']['type'] , 'Arquivo');

    $params = [
        'arquive' => $cfile,
        'template' => $post['template'],
        'acao' => $post['type'],
        'segmentacao' => $post['segmentation'],
    ];

    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_INFILESIZE => $_FILES['arquive']['size'],
        CURLOPT_POSTFIELDS => $params,
        CURLOPT_HTTPHEADER => array(
            "Authorization: Basic ****:****",
            "Cache-Control: no-cache",
            "Connection: keep-alive",
            "Content-Type: multipart/form-data;",
        ),
    ]);

    $content = curl_exec($ch);

    if ($content === false) {
        throw new Exception(curl_error($ch), curl_errno($ch));
    }

    curl_close($ch);

} catch(Exception $e) {
    trigger_error(sprintf( 'Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()),E_USER_ERROR);
}

I tested pass the file based on other solutions I found here, as:

    ...'arquive' => curl_create_file($file)...
    ...'arquive' => "@".$file...

And I ended up leaving the same way.

I spent a long time researching the solution but unfortunately I could not, I repeated several times adapting to examples I found here, but I did not succeed.

1 answer

0


Just to leave registered and for users query, I managed to solve the problem.

I thought right what I was doing, I was allocating the file in a local folder and passing the path $file in the first parameter for the class CURLFile for sending the same, all within the same request.
$file = "./src/temp/{$_FILES['arquive']['name']}";

$cfile = new CURLFile($file , $_FILES['arquive']['type'] , 'Arquivo');

I started using $_FILES['arquive']['tmp_name'] which stores the value where the file is temporarily stored and worked

$cfile = new CURLFile($_FILES['arquive']['tmp_name'] , $_FILES['arquive']['type'] , 'Arquivo');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.