2009年6月6日 星期六

[引用]php 用 CURL 把檔案丟到另一台 SERVER

來源

##
$boundary = uniqid('------------------');
$MPboundary = '--'.$boundary;
$endMPboundary = $MPboundary. '--';

// Create multipart form post per Yahoo! Photos and RFC 1867

// Fetch a photo (you could use a form upload for this)
$filename='./mail/test.csv';
$file = file_get_contents($filename);

// Create the multipart body. The name and filename are required but are just placeholders.

$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'Content-Disposition: form-data; name="filename"; filename="test.csv"'. "\r\n";
$multipartbody .= 'Content-Type: text/csv'. "\r\n\r\n";
$multipartbody .= $file;
//$multipartbody.=$str."\r\n";
foreach($post_data as $k => $v)
{
$multipartbody .= $MPboundary . "\r\n";
$multipartbody.='content-disposition: form-data; name="'.$k."\r\n\r\n";
$multipartbody.=$v."\r\n";
//$url_en_post_data[urlencode($k)]=urlencode($v);
}
$multipartbody .= "\r\n". $endMPboundary;


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://test/getfile.php?' );
curl_setopt($ch , CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS,$multipartbody );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$msg = curl_exec($ch);


在另一台接收檔案的 server 上,放一個getfile.php跟 php.net 的code 一樣
丟檔案的 server 上執行上面的 code ,其中$post_data 就是其他要丟的欄位

【下列文章您可能也有興趣】

沒有留言: