来源:https ://drupal.org/user/194073
<?php
$boundary = md5(uniqid());
$post_data = array(
'name' => 'Ayesh',
'file' => '/var/www/test/test.png',
);
$options = array(
'method' => 'POST',
'data' => multipart_encode($boundary, $post_data),
'timeout' => $connect_ariba_values['timeout'],
'headers' => array('Content-Type' => "multipart/form-data; boundary=$boundary"),
);
$response = drupal_http_request($url, $options);
// Function to encode text data.
function multipart_enc_text($name, $value){
return "Content-Disposition: form-data; name=\"$name\"\r\n\r\n$value\r\n";
}
// Function to multipart encode a file from a give path.
function multipart_enc_file($path){
if (substr($path, 0, 1) == "@") $path = substr($path, 1);
$filename = basename($path);
$mimetype = "application/octet-stream";
$data = "Content-Disposition: form-data; name=\"file\"; filename=\"$filename\"\r\n"; // "file" key.
$data .= "Content-Transfer-Encoding: binary\r\n";
$data .= "Content-Type: $mimetype\r\n\r\n";
$data .= file_get_contents($path) . "\r\n";
return $data;
}
// base function to encode a data array.
function multipart_encode($boundary, $params){
$output = "";
foreach ($params as $key => $value){
$output .= "--$boundary\r\n";
if ($key == 'file'){
$output .= multipart_enc_file($value);
} else $output .= multipart_enc_text ($key, $value);
}
$output .="--$boundary--";
return $output;
}
在服务器中,文件将在 中可用$_FILES['file']
,其他POST
数据将在其相关键中可用。
我没有测试代码。我刚刚在 drupal 论坛中找到了代码,将其更改为与 Drupal 6 和 7 一起使用。