Skip to content

Commit

Permalink
Upload files
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor authored Apr 20, 2022
1 parent 6c73788 commit 648375c
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions Protocols/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit 648375c

Please sign in to comment.