Home > Backend Development > PHP Tutorial > Use CURL to upload files to the server

Use CURL to upload files to the server

巴扎黑
Release: 2016-11-22 16:58:26
Original
1669 people have browsed it

1. Client-side PHP code

<?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);
Copy after login

2. Server-side code

<?php
if(!isset($_FILES[&#39;client_file&#39;]) || $_FILES[&#39;client_file&#39;][&#39;error&#39;] > 0){
            $arrRet[&#39;error_no&#39;] = -1;
            $arrRet[&#39;data&#39;] = $arrRet[&#39;data&#39;] = array(
                &#39;msg&#39; => "upload file failed",
            );
        }
$arrInput = array(
                &#39;filename&#39; => $_FILES[&#39;client_file&#39;][&#39;name&#39;],
                &#39;tmp_name&#39; => $_FILES[&#39;client_file&#39;][&#39;tmp_name&#39;],
                &#39;type&#39; => $_FILES[&#39;file&#39;][&#39;type&#39;],
            );
move_uploaded_file($arrInput[&#39;tmp_name&#39;],ROOT_PATH."/data/app/client/bin/".$arrInput[&#39;filename&#39;]);
Copy after login

3. Upload using web page

<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>
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template