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

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

你可能感兴趣的:(PHP7 cURL上传文件至七牛 curl_setopt(): Disabling safe uploads is no longer supported)