天天看點

php curl post 上傳檔案到伺服器

<!--<html> 
<body> 
<form action="http://222.161.206.158:8136/ispirit/im/upload.php" method="post" enctype="multipart/form-data"> 
<input type="text"name='P' value = 1 ></input> 
<input type="text"name='MSG_CATE' value = 'file'></input>
<input type="text"name='UPLOAD_MODE' value = 1 ></input>
<input type="text" name="DEST_UID" value = 1></input> 
<input type="file" name="ATTACHMENT"></input> 
<input type="submit" ></input> 
</body> 
</html>
-->
<?php
   
    
    
    function curl_form($post_data,$sumbit_url,$http_url){
        
        //初始化
        $ch = curl_init();
        //設定變量
        curl_setopt($ch, CURLOPT_URL, $sumbit_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//執行結果是否被傳回,0是傳回,1是不傳回
        curl_setopt($ch, CURLOPT_HEADER, 0);//參數設定,是否顯示頭部資訊,1為顯示,0為不顯示
        curl_setopt($ch, CURLOPT_REFERER, $http_url); 
        //表單資料,是正規的表單設定值為非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);
        //執行并擷取結果
        $output = curl_exec($ch);
        if($outopt === FALSE)
        {
            echo "<br/>","cUrl Error:".curl_error($ch);
        }
        //    釋放cURL句柄
        curl_close($ch); 
		
		echo $output;
		print_r($output);
    }
    
    
    $furl = "@d:\MYOA\aaa.doc";
              // $furl1 = "@H:/htdocs/jiyiji.php";
               $post_data = array (
                   "P"=>'1',
				    "MSG_CATE"=>'file',
					 "UPLOAD_MODE"=>'1',
					  "DEST_UID"=>'1',
                   "ATTACHMENT" => $furl
               );
    $sumbit_url = "http://222.161.206.158:8136/ispirit/im/upload.php"; 
   // $http_url="http://www.baidu.com";
    curl_form($post_data,$sumbit_url,$http_url);
?>