1. 程式人生 > >PHP中curl模擬post上傳及接收檔案

PHP中curl模擬post上傳及接收檔案

public function Action_Upload(){

     $this->path_config();

    exit();

    $furl="@d:\develop\JMFrameworkWithDemo.rar";

    $url= "http://localhost/DemoIndex/curl_pos/";

    $this->upload_file_to_cdn($furl, $url);

}

public function upload_file_to_cdn($furl,$url){

    //  初始化

    $ch = curl_init();

    
// 要上傳的本地檔案地址"@F:/xampp/php/php.ini"上傳時候,上傳路徑前面要有@符號 $post_data = array ( "upload" => $furl ); //print_r($post_data); //CURLOPT_URL 是指提交到哪裡?相當於表單裡的“action”指定的路徑 //$url = "http://localhost/DemoIndex/curl_pos/"; // 設定變數 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt(
$ch, CURLOPT_RETURNTRANSFER, 0);//執行結果是否被返回,0是返回,1是不返回 curl_setopt($ch, CURLOPT_HEADER, 0);//引數設定,是否顯示頭部資訊,1為顯示,0為不顯示 //偽造網頁來源地址,偽造來自百度的表單提交 curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com"); //表單資料,是正規的表單設定值為非0 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100);//
設定curl執行超時時間最大是多少 //使用陣列提供post資料時,CURL元件大概是為了相容@filename這種上傳檔案的寫法, //預設把content_type設為了multipart/form-data。雖然對於大多數web伺服器並 //沒有影響,但是還是有少部分伺服器不相容。本文得出的結論是,在沒有需要上傳檔案的 //情況下,儘量對post提交的資料進行http_build_query,然後傳送出去,能實現更好的相容性,更小的請求資料包。 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // 執行並獲取結果 curl_exec($ch); if(curl_exec($ch) === FALSE) { echo "<br/>"," cUrl Error:".curl_error($ch); } // 釋放cURL控制代碼 curl_close($ch); echo "aaa45"; } function action_curl_pos(){ var_dump($_FILES); $aa= move_uploaded_file($_FILES["upload"]["tmp_name"], "/wamp/tools/1.rar"); if($aa){ echo "11"; } }