• 每天进步一点点!

文章分类

推荐网站

常用手册

关于php 5.5 CURLFile上传的问题【原创】

<<返回

2015-11-25 19:21:44

今天用多贝云的API上传课件时,报了如下的错误,这个是是在PHP5.5中出现的问题,原来是PHP5.5废弃了’@’这种模式。
The usage of the @filename API for file uploading is deprecated. Please use the CURLFile class instead
PHP的cURL支持通过给CURL_POSTFIELDS传递关联数组(而不是字符串)来生成multipart/form-data的POST请求。

传统上,PHP的cURL支持通过在数组数据中,使用“@+文件全路径”的语法附加文件,供cURL读取上传。这与命令行直接调用cURL程序的语法是一致的:

curl_setopt(ch, CURLOPT_POSTFIELDS, array(
    'file' => '@'.realpath('image.png'), 
)); 
equals
$ curl -F "file=@/absolute/path/to/image.png" 

 

PHP 5.5另外引入了CURL_SAFE_UPLOAD选项,可以强制PHP的cURL模块拒绝旧的@语法,仅接受CURLFile式的文件。5.5的默认值为false,5.6的默认值为true。

 

解决方法:

对上传文件调用curl_file_create()创建CURLFile,代码如下:

	public function uploadDocument($filename) {
		$params = $this->prepareParameters(array());
		#$params["slidesFile"] = "@" . $filename;
		$params['slidesFile'] = curl_file_create($filename);//使用这种方式
		
		$path = "/api/v3/documents/upload";
		$result = $this->post($path, $params);
		return $result;
	}

 

 

 

文章评论

  • 暂无评论

发表评论

昵称:

内容:

发表评论