PHP CURL:不建议使用@filename API进行文件上传 [英] PHP CURL: The usage of the @filename API for file uploading is deprecated

查看:200
本文介绍了PHP CURL:不建议使用@filename API进行文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此消息:

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

我知道,我可以使用CURLFile类重写代码,但是只能从5.5版开始.

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

我的网站必须必须在PHP 5.3,PHP 5.4或PHP 5.5上运行,因此我不能放弃5.3和5.4的兼容性.所以我不能使用CURLFile.

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.

如何重写代码以使其在任何PHP上运行而无需任何PHP版本检查?

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

推荐答案

我发现的最佳解决方案是/src/Guzzle/Http/Message/PostFile.php :

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;
}

这篇关于PHP CURL:不建议使用@filename API进行文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆