使用CURL把檔案上傳至伺服器

來源:互聯網
上載者:User
一、用戶端的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);

二、伺服器端代碼

<?phpif(!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>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.