PHP7 cURL上传文件至七牛 curl_setopt(): Disabling safe uploads is no longer supported

在做php5.3升级到php7.1时出了点小问题,使用curl上传素材文件到七牛时
提示:

运行时会出现以下错误:curl_setopt(): Disabling safe uploads is no longer supported

意思时该设置项已经不被支持。
之后在官方文档上找到
TRUE to disable support for the @ prefix for uploading files inCURLOPT_POSTFIELDS, which means that values starting with @can be safely passed as fields. CURLFile may be used for uploads instead.

  • 于是尝试使用CURLFile(PHP5.5以上开始支持)
    具体代码如下:
    /**
     * 上传七牛
     * @param string $scope
     * @param string $file
     * @param string $key
     * @return mixed
     */
    public function qiniu_uploads($scope = '', $file = '', $key = '') {
        $access_key = QINIU_ACCESS_KEY;
        $secret_key = QINIU_SECRET_KEY;
        $b = json_encode(array('scope' => $scope, 'deadline' => time() + 3600));
        $token = $access_key . ':' . str_replace(array('+', '/'), array('-', '_'), base64_encode(hash_hmac('sha1', str_replace(array('+', '/'), array('-', '_'), base64_encode($b)), $secret_key, true)) . ':' . str_replace(array('+', '/'), array('-', '_'), base64_encode($b)));
        $fields = array('token' => $token, 'file' =>  new \CURLFile($file), 'key' => $key);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://up.qiniu.com/');
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }

最终成功将文件上传至七牛

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容