发现我的个人网站开启了curl 但是无法对外连接任何端口,所以还是有方法的http://,使用vps搭建临时的服务器,使用php curl 上传文件到服务器 使用网上的样例出现 deprecated : curl_setopt(): the usage of the @filename api for file uploading is deprec
发现我的个人网站开启了curl 但是无法对外连接任何端口,所以还是有方法的http://,使用vps搭建临时的服务器,使用php curl 上传文件到服务器
使用网上的样例出现
deprecated: curl_setopt(): the usage of the @filename api for file uploading is deprecated. please use the curlfile class instead in/usr/local/apache2/htdocs/t1/php_curl_up1_1.php on line41
好吧,这个是版本问题导致的
于是找到了最新的在线文档说明和例子
http://www.php.net/manual/zh/function.curl-setopt.php
范例 ?
example #1 初始化一个新的curl会话并获取一个网页
example #2 上传文件
'foo','file'=>'@/home/user/test.png');
curl_setopt($ch,curlopt_url,'http://localhost/upload.php');
curl_setopt($ch,curlopt_post,1);
curl_setopt($ch,curlopt_postfields,$data);
curl_exec($ch);
?>
以上例程会输出:
array( [name] => foo)array( [file] => array ( [name] => test.png [type] => image/png [tmp_name] => /tmp/phpcpjneq [error] => 0 [size] => 279 ))
注释 ?note:
传递一个数组到curlopt_postfields,curl会把数据编码成multipart/form-data,而然传递一个url-encoded字符串时,数据会被编码成application/x-www-form-urlencoded。
于是我照葫芦画瓢:
上传文件的文件
$cfile);
curl_setopt($ch, curlopt_post,1);
curl_setopt($ch, curlopt_postfields, $data);
curl_setopt($ch, curlopt_infilesize,filesize($file)); //这句非常重要,告诉远程服务器,文件大小,查到的是前辈的文章http://blog.csdn.net/cyuyan112233/article/details/21015351
// execute the handle
curl_exec($ch);
?>
接收文件的文件内容
这样就可以啦!~~哈哈~