当前位置:Gxlcms > PHP教程 > 使用CURL把文件上传至服务器

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

时间:2021-07-01 10:21:17 帮助过:55人阅读

一、客户端的PHP代码

<?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);

二、服务器端代码

<?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']);

三、使用网页上传的方式

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

人气教程排行