function set_post_files($array, $parent_key = null) { $temp = array(); foreach ($array as $key => $value) { $key = isset($parent_key) ? sprintf('%s[%s]', $parent_key, urlencode($key)) : urlencode($key); if (is_array($value)) { $temp = array_merge_recursive($temp, set_post_files($value, $key)); } else { if (preg_match('#^([^\\[\\]]+)\\[(name|type|tmp_name)\\]#', $key, $m)) { $temp[str_replace($m[0], $m[1], $key)][$m[2]] = $value; } } } return $temp; }
} else { if (list($_basic_auth_realm, $_basic_auth_header) = each($_auth_creds)) { $_request_headers .= "Authorization: Basic {$_basic_auth_header}\r\n"; } } } if ($_request_method == 'POST') { if (!empty($_FILES) && $_system['uploads']) { $_data_boundary = '----' . md5(uniqid(rand(), true)); $array = set_post_vars($_POST); foreach ($array as $key => $value) { $_post_body .= "--{$_data_boundary}\r\n"; $_post_body .= "Content-Disposition: form-data; name=\"{$key}\"\r\n\r\n"; $_post_body .= urldecode($value) . "\r\n"; } $array = set_post_files($_FILES); foreach ($array as $key => $file_info) { $_post_body .= "--{$_data_boundary}\r\n"; $_post_body .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file_info['name']}\"\r\n"; $_post_body .= 'Content-Type: ' . (empty($file_info['type']) ? 'application/octet-stream' : $file_info['type']) . "\r\n\r\n"; if (is_readable($file_info['tmp_name'])) { $handle = fopen($file_info['tmp_name'], 'rb'); $_post_body .= fread($handle, filesize($file_info['tmp_name'])); fclose($handle); } $_post_body .= "\r\n"; } $_post_body .= "--{$_data_boundary}--\r\n"; $_request_headers .= "Content-Type: multipart/form-data; boundary={$_data_boundary}\r\n"; $_request_headers .= "Content-Length: " . strlen($_post_body) . "\r\n\r\n"; $_request_headers .= $_post_body;