PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

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

巴扎黑
巴扎黑 原创
2016-11-22 16:58:26 1283浏览

一、客户端的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>


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。