From 275f6f0c14f8cc41874548559eb0c165199d2415 Mon Sep 17 00:00:00 2001 From: steering7253 Date: Wed, 24 Jun 2026 10:15:07 +0000 Subject: support either file[] or file as the field name --- upload.php | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'upload.php') diff --git a/upload.php b/upload.php index c13b193..01e3c92 100644 --- a/upload.php +++ b/upload.php @@ -7,19 +7,15 @@ if (isset($_GET['df'])) { exit; } if ($_SERVER['REQUEST_METHOD'] != 'POST' && $_SERVER['REQUEST_METHOD'] != 'PUT') { + header('Status: 405 Method Not Allowed'); + echo "You must use PUT or POST to upload a file.\n"; exit; } $c_t = strtolower($_SERVER['HTTP_CONTENT_TYPE'] ?? ''); if ($c_t == 'multipart/form-data' || 0 === strpos($c_t, 'multipart/form-data;')) { - foreach (array_keys($_FILES['file']['size']) as $index) { - // remap the array to have the dimensions ordered properly. - $file = array(); - foreach (array_keys($_FILES['file']) as $key) { - $file[$key] = $_FILES['file'][$key][$index]; - } - // good, now $file['tmp_name'] exists for example, instead of $_FILES['file']['tmp_name'][$index]... - + $FILES = remap_files(); + foreach ($FILES['file'] as $file) { if (!is_uploaded_file($file['tmp_name'])) { die_error("Not an uploaded file: $file[tmp_name]"); } @@ -96,3 +92,22 @@ function get_extension($file) { } return $ext; } + +function remap_files() { + $files = array(); + foreach (array_keys($_FILES) as $input_name) { + $files[$input_name] = array(); + foreach (array_keys($_FILES[$input_name]) as $file_info_key) { + if (is_array($_FILES[$input_name][$file_info_key])) { + foreach ($_FILES[$input_name][$file_info_key] as $i => $v) { + if (!isset($files[$input_name][$i])) $files[$input_name][$i] = array(); + $files[$input_name][$i][$file_info_key] = $v; + } + } else { + if (!isset($files[$input_name][0])) $files[$input_name][0] = array(); + $files[$input_name][0][$file_info_key] = $_FILES[$input_name][$file_info_key]; + } + } + } + return $files; +} -- cgit v1.3.1-10-gc9f91