Débasish Nayak
Débasish Nayak

Reputation: 86

curl_setopt() throwing an error exception while trying to upload a file by curl in php

I found some questions related to the above but i didn't get any appropriate answer. Here's my code:

$requestUrl = <MY_URL>;
$filePath = <MY_FILE_PATH>;
$post = array('extra_info' => '123456','file_contents'=>'@'.$filePath);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$requestUrl);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
curl_close($ch);

echo $response;

I am getting an error exception like:

curl_setopt(): The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead.

Any help will be appreciated !

Upvotes: 0

Views: 718

Answers (2)

Prabhat Rai
Prabhat Rai

Reputation: 799

As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. You can check the manual here

Try this:

$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST" ); 
curl_setopt($ch, CURLOPT_HTTPHEADER,  
array('Authorization: Bearer XXXXXXXXXXXXXXXYYYYYzzzzzz', 'Accept-Version: ~1', 'Except: 100-continue', 'Content-type: multipart/form-data')); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);

$args['file'] = new CurlFile('<file_path>', 'text/plain', 'myfile');

curl_setopt($ch, CURLOPT_POSTFIELDS, $args); 

$response = curl_exec($ch);

Upvotes: 2

Naincy
Naincy

Reputation: 2943

Please refer this, May this help you http://pdfcrowd.com/forums/read.php?4,1930

PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax Also See: https://wiki.php.net/rfc/curl-file-upload

public function getCurlValue()
{

    if (function_exists('curl_file_create')) {
        return curl_file_create($this->filename, $this->contentType, $this->postname);
    }

    // Use the old style if using an older version of PHP
    $value = "@{$this->filename};filename=" . $this->postname;
    if ($this->contentType) {
        $value .= ';type=' . $this->contentType;
    }

    return $value;
}

Upvotes: 0

Related Questions