Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP CURL: The usage of the @filename API for file uploading is deprecated

Tags:

php

I got this message:

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

I know that I may rewrite my code using CURLFile class, but it's awailable only from 5.5.

My site must run on PHP 5.3, PHP 5.4 or PHP 5.5, so I can't drop 5.3 and 5.4 compatibility. So I can't use CURLFile.

How can I rewrite code to make it run on any PHP without any PHP version checks?

like image 397
Dmitry Avatar asked Jan 07 '14 13:01

Dmitry


2 Answers

The best solution I have found is /src/Guzzle/Http/Message/PostFile.php:

public function getCurlValue()
{
    // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
    // See: https://wiki.php.net/rfc/curl-file-upload
    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;
}
like image 144
Dmitry Avatar answered Nov 15 '22 07:11

Dmitry


I believe this will allow you to use the old way without throwing a warning (if simply suppressing via @ is not acceptable):

curl_setopt($curl_handle, CURLOPT_SAFE_UPLOAD, false);

See here

like image 20
Drew Noel Avatar answered Nov 15 '22 06:11

Drew Noel