Fix dropped folder uploads
This commit is contained in:
@@ -8,11 +8,16 @@ if (!is_dir($target['path'])) {
|
||||
}
|
||||
|
||||
$uploaded = [];
|
||||
foreach ($_FILES['files']['name'] ?? [] as $index => $name) {
|
||||
if ($_FILES['files']['error'][$index] !== UPLOAD_ERR_OK) {
|
||||
$names = unav_upload_array($_FILES['files']['name'] ?? []);
|
||||
$errors = unav_upload_array($_FILES['files']['error'] ?? []);
|
||||
$tmpNames = unav_upload_array($_FILES['files']['tmp_name'] ?? []);
|
||||
$relativePaths = unav_upload_array($_POST['paths'] ?? []);
|
||||
foreach ($names as $index => $name) {
|
||||
if (($errors[$index] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
|
||||
unav_error(400, 'Upload failed');
|
||||
}
|
||||
$safeName = unav_upload_safe_relative_path((string)$name);
|
||||
$relativeName = $relativePaths[$index] ?? $name;
|
||||
$safeName = unav_upload_safe_relative_path((string)$relativeName);
|
||||
$relativeParent = dirname($safeName);
|
||||
if ($relativeParent !== '.') {
|
||||
$parentCandidate = $target['path'] . '/' . $relativeParent;
|
||||
@@ -28,7 +33,7 @@ foreach ($_FILES['files']['name'] ?? [] as $index => $name) {
|
||||
if (!is_dir(dirname($destination['path']))) {
|
||||
unav_error(500, 'Could not create upload directory');
|
||||
}
|
||||
if (!move_uploaded_file($_FILES['files']['tmp_name'][$index], $destination['path'])) {
|
||||
if (!move_uploaded_file($tmpNames[$index] ?? '', $destination['path'])) {
|
||||
unav_error(500, 'Could not store uploaded file');
|
||||
}
|
||||
$uploaded[] = [
|
||||
@@ -39,6 +44,16 @@ foreach ($_FILES['files']['name'] ?? [] as $index => $name) {
|
||||
|
||||
unav_json(['uploaded' => $uploaded], 201);
|
||||
|
||||
function unav_upload_array($value): array {
|
||||
if (is_array($value)) {
|
||||
return array_values($value);
|
||||
}
|
||||
if ($value === null || $value === '') {
|
||||
return [];
|
||||
}
|
||||
return [$value];
|
||||
}
|
||||
|
||||
function unav_upload_safe_relative_path(string $name): string {
|
||||
$parts = [];
|
||||
foreach (preg_split('#[/\\\\]+#', $name) ?: [] as $part) {
|
||||
|
||||
Reference in New Issue
Block a user