From 648375c317ecbe39c60ba31e64c69d8abcf2881b Mon Sep 17 00:00:00 2001 From: walkor Date: Wed, 20 Apr 2022 21:55:45 +0800 Subject: [PATCH] Upload files --- Protocols/Http/Request.php | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/Protocols/Http/Request.php b/Protocols/Http/Request.php index d9f4e9235..ae8ccb0d4 100644 --- a/Protocols/Http/Request.php +++ b/Protocols/Http/Request.php @@ -508,14 +508,14 @@ protected function parseUploadFiles($http_post_boundary) if ($boundary_data_array[0] === '' || $boundary_data_array[0] === "\r\n") { unset($boundary_data_array[0]); } - $key = -1; + $index = -1; $files = array(); $post_str = ''; foreach ($boundary_data_array as $boundary_data_buffer) { list($boundary_header_buffer, $boundary_value) = \explode("\r\n\r\n", $boundary_data_buffer, 2); // Remove \r\n from the end of buffer. $boundary_value = \substr($boundary_value, 0, -2); - $key++; + $index++; foreach (\explode("\r\n", $boundary_header_buffer) as $item) { list($header_key, $header_value) = \explode(": ", $item); $header_key = \strtolower($header_key); @@ -537,11 +537,11 @@ protected function parseUploadFiles($http_post_boundary) $error = UPLOAD_ERR_CANT_WRITE; } } - if (!isset($files[$key])) { - $files[$key] = array(); + if (!isset($files[$index])) { + $files[$index] = array(); } // Parse upload files. - $files[$key] += array( + $files[$index] += array( 'key' => $match[1], 'name' => $match[2], 'tmp_name' => $tmp_file, @@ -554,25 +554,33 @@ protected function parseUploadFiles($http_post_boundary) else { // Parse $_POST. if (\preg_match('/name="(.*?)"$/', $header_value, $match)) { - $key = $match[1]; - $post_str .= \urlencode($key)."=".\urlencode($boundary_value).'&'; + $k = $match[1]; + $post_str .= \urlencode($k)."=".\urlencode($boundary_value).'&'; } } break; case "content-type": // add file_type - if (!isset($files[$key])) { - $files[$key] = array(); + if (!isset($files[$index])) { + $files[$index] = array(); } - $files[$key]['type'] = \trim($header_value); + $files[$index]['type'] = \trim($header_value); break; } } } - foreach ($files as $file) { + $files_unique = array(); + foreach ($files as $index => $file) { + $key = $file['key']; + if (\substr($key, -2) === '[]') { + $key = $index; + } + $files_unique[$key] = $file; + } + foreach ($files_unique as $file) { $key = $file['key']; unset($file['key']); - $str = \urlencode($key)."=1"; + $str = \urlencode($key) . "=1"; $result = []; \parse_str($str, $result); \array_walk_recursive($result, function(&$value) use ($file) {