diff options
| author | 2026-06-24 10:15:07 +0000 | |
|---|---|---|
| committer | 2026-06-24 10:15:07 +0000 | |
| commit | 275f6f0c14f8cc41874548559eb0c165199d2415 (patch) | |
| tree | 80e0488e4ab1bb73f9cf1bd352b7fcab58e3a9b6 | |
| parent | add 405 (diff) | |
support either file[] or file as the field name
| -rw-r--r-- | upload.php | 31 |
1 files changed, 23 insertions, 8 deletions
@@ -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; +} |
