使用CURL把文件上传至服务器

[size=large]一、客户端的PHP代码[/size]

<?php

//初始化一个句柄
$ch = curl_init();

//设置访问地址
curl_setopt($ch, CURLOPT_URL, "http://cq01-testing-lv01.vm.baidu.com:8808/mobile/uploadclient");

//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

//参数设置,是否显示头部信息,1为显示,0为不显示
curl_setopt($ch, CURLOPT_HEADER, 0);

//伪造网页来源地址,伪造来自百度的表单提交
//curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com");

//设置这个是POST请求
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);

//furl中的值必须以@符号开头,@后面是你的相对或者绝对路径
$furl="@./a.php";
$post_data = array (
"client_file" => $furl
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_exec($ch);

if(FALSE === curl_exec($ch)){

echo "cUrl Error:".curl_error($ch);
}else{

echo "upload success";
}
//释放cURL句柄
curl_close($ch);



[size=large]二、服务器端代码[/size]


<?php

if(!isset($_FILES['client_file']) || $_FILES['client_file']['error'] > 0){

$arrRet['error_no'] = -1;
$arrRet['data'] = $arrRet['data'] = array(
'msg' => "upload file failed",
);
}
$arrInput = array(
'filename' => $_FILES['client_file']['name'],
'tmp_name' => $_FILES['client_file']['tmp_name'],
'type' => $_FILES['file']['type'],
);

move_uploaded_file($arrInput['tmp_name'],ROOT_PATH."/data/app/client/bin/".$arrInput['filename']);


[size=large]
三、使用网页上传的方式[/size]


<form action="/upload.php" method="post" enctype="multipart/form-data">
<label>上传文件: <input name="client_file" type="file"/></label>
<input name="submit" type="submit" value="提交"/>
</form>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值