首页 > php教程 > php手册 > php5.5.9 curl 上传文件

php5.5.9 curl 上传文件

WBOY
发布: 2016-06-06 19:53:26
原创
1009 人浏览过

发现我的个人网站开启了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会话并获取一个网页

<span><?php <br> </span><span>// 创建一个新cURL资源<br> </span><span>$ch </span><span>=</span><span>curl_init</span><span>();<br> <br> </span><span>// 设置URL和相应的选项<br> </span><span>curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_URL</span><span>,</span><span>"http://www.example.com/"</span><span>);<br> </span><span>curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_HEADER</span><span>,</span><span>false</span><span>);<br> <br> </span><span>// 抓取URL并把它传递给浏览器<br> </span><span>curl_exec</span><span>(</span><span>$ch</span><span>);<br> <br> </span><span>//关闭cURL资源,并且释放系统资源<br> </span><span>curl_close</span><span>(</span><span>$ch</span><span>);<br> </span><span>?></span>

Example #2 上传文件

<span><?php <br> <br> </span><span>/* http://localhost/upload.php:<br> print_r($_POST);<br> print_r($_FILES);<br> */<br> <br> </span><span>$ch </span><span>=</span><span>curl_init</span><span>();<br> <br> </span><span>$data </span><span>= array(</span><span>'name'</span><span>=></span><span>'Foo'</span><span>,</span><span>'file'</span><span>=></span><span>'@/home/user/test.png'</span><span>);<br> <br> </span><span>curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_URL</span><span>,</span><span>'http://localhost/upload.php'</span><span>);<br> </span><span>curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_POST</span><span>,</span><span>1</span><span>);<br> </span><span>curl_setopt</span><span>(</span><span>$ch</span><span>,</span><span>CURLOPT_POSTFIELDS</span><span>,</span><span>$data</span><span>);<br> <br> </span><span>curl_exec</span><span>(</span><span>$ch</span><span>);<br> </span><span>?></span>

以上例程会输出:

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


于是我照葫芦画瓢:

上传文件的文件

// Path to the file we want to upload
$uploaddir = getcwd();
$file = $uploaddir."/9N0CB31K28JU0007.jpg"; //这里非常重要!一定要绝对地址才行,所以使用这个拼接成了绝对地址


// Create a cURL handle
$ch = curl_init('http://192.168.5.41/T1/up2.php');


// Create a CURLFile object
$cfile = curl_file_create($file);


// Assign POST data
$data = array('fff' => $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);




?> 






接收文件的文件内容

print_r($_FILES);
$uploaddir = getcwd().'/tmp/'; //a directory inside 
echo $uploaddir."
";
echo $_FILES["fff"]["name"]."
";
$file_name=basename($_FILES["fff"]["name"]);
echo $file_name."
";
move_uploaded_file($_FILES['fff']['tmp_name'],$uploaddir.$file_name);


//测试是否有写入权限
//$fp=fopen("tmp.txt","wb");
//fwrite($fp,"abc\r\n");
//fclose($fp);
?>



这样就可以啦!~~哈哈~

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板