abstract:本文實例講述了PHP使用curl模擬post上傳及接收文件的方法。分享給大家供大家參考,具體如下:public function Action_Upload(){ $this->path_config(); exit(); &nb
本文實例講述了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 是指提交到哪里?相當(dāng)于表單里的“action”指定的路徑 //$url = "http://localhost/DemoIndex/curl_pos/"; // 設(shè)置變量 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//執(zhí)行結(jié)果是否被返回,0是返回,1是不返回 curl_setopt($ch, CURLOPT_HEADER, 0);//參數(shù)設(shè)置,是否顯示頭部信息,1為顯示,0為不顯示 //偽造網(wǎng)頁來源地址,偽造來自百度的表單提交 curl_setopt($ch, CURLOPT_REFERER, "http://www.baidu.com"); //表單數(shù)據(jù),是正規(guī)的表單設(shè)置值為非0 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100);//設(shè)置curl執(zhí)行超時時間最大是多少 //使用數(shù)組提供post數(shù)據(jù)時,CURL組件大概是為了兼容@filename這種上傳文件的寫法, //默認把content_type設(shè)為了multipart/form-data。雖然對于大多數(shù)web服務(wù)器并 //沒有影響,但是還是有少部分服務(wù)器不兼容。本文得出的結(jié)論是,在沒有需要上傳文件的 //情況下,盡量對post提交的數(shù)據(jù)進行http_build_query,然后發(fā)送出去,能實現(xiàn)更好的兼容性,更小的請求數(shù)據(jù)包。 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // 執(zhí)行并獲取結(jié)果 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"; } }
更多關(guān)于PHP使用curl模擬post上傳及接收文件的方法請關(guān)注PHP中文網(wǎng)(m.miracleart.cn)其他文章!