Heim >Backend-Entwicklung >PHP-Tutorial >Verwenden Sie CURL, um Dateien auf den Server hochzuladen
1. Clientseitiger 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);
2. Serverseitiger Code
<?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']);
3. Hochladen über die Webseite
<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>