Fix: Bild-Uploads wieder funktionsfähig machen

- Upload-Limits auf 25 MB pro Bild bzw. 64 MB pro Request erhöht
  (public/.user.ini für FPM/CGI, docker/php/uploads.ini für Docker)
- Docker: storage/-Verzeichnis beim Start auf www-data umstellen
- Image::store() prüft Upload-Fehlercode, Bildformat, GD/JPEG und
  Schreibrechte und wirft verständliche Fehler statt still zu scheitern
- Galerie, Benutzer- und Tour-Bild zeigen Fehler als Flash-Meldung
- post_max_size-Überschreitung wird vor der CSRF-Prüfung abgefangen
- Slug wandelt Großbuchstaben-Umlaute korrekt um
- Diagnose-Script bin/check-upload.php ergänzt
This commit is contained in:
torsten 2026-11-08 14:32:36 +01:00
parent 8bde8d143e
commit d8c9ec8ef8
16 changed files with 486 additions and 35 deletions

View file

@ -29,9 +29,11 @@ class GalleryController
$tourName = db()->fetchValue('SELECT name FROM tours WHERE id = ?', [(int) $tour_id]) ?: '';
view('tour.gallery.show', [
'tour_id' => $tour_id,
'tour_name' => $tourName,
'imagelist' => $imagelist,
'tour_id' => $tour_id,
'tour_name' => $tourName,
'imagelist' => $imagelist,
'upload_ready' => $this->isUploadReady('tour_' . $tour_id),
'upload_limit' => ini_get('upload_max_filesize'),
]);
}
@ -56,23 +58,64 @@ class GalleryController
public function store(string $tour_id): void
{
validate(['files' => 'required']);
// Gleiche Berechtigung wie bei der Galerie-Ansicht: Admin oder gebuchte Tour.
$user = Auth::user();
$files = $_FILES['files'] ?? [];
$tmpNames = $files['tmp_name'] ?? [];
if ($user === null) {
redirect('/login');
}
foreach ($tmpNames as $i => $tmpName) {
if (empty($tmpName) || !is_uploaded_file($tmpName)) {
if (!$user->isAdmin() && Booking::forUser((int) $tour_id, (int) $user->id) === null) {
redirect('/tour');
}
if (!isset($_FILES['files']) || !is_array($_FILES['files']['name'] ?? null)) {
flash_set('error', 'Bitte mindestens eine Bilddatei auswählen.');
redirect('/tour/gallery/' . $tour_id);
}
$names = $_FILES['files']['name'];
$tmpNames = $_FILES['files']['tmp_name'] ?? [];
$errors = $_FILES['files']['error'] ?? [];
$sizes = $_FILES['files']['size'] ?? [];
$stored = 0;
$problems = [];
foreach (array_keys($names) as $i) {
$upload = [
'name' => (string) ($names[$i] ?? ''),
'tmp_name' => (string) ($tmpNames[$i] ?? ''),
'error' => (int) ($errors[$i] ?? UPLOAD_ERR_NO_FILE),
'size' => (int) ($sizes[$i] ?? 0),
];
// Leere Slots überspringen, echte Fehler aber melden.
if ($upload['error'] === UPLOAD_ERR_NO_FILE) {
continue;
}
$name = $files['name'][$i] ?? '';
$upload = [
'tmp_name' => $tmpName,
'name' => $name,
];
try {
Image::store($upload, 'tour_' . $tour_id, $upload['name']);
$stored++;
} catch (\Throwable $e) {
if (!$e instanceof \RuntimeException) {
error_log('Galerie-Upload: ' . $e);
$problems[] = 'Ein Bild konnte nicht verarbeitet werden.';
} else {
$problems[] = $e->getMessage();
}
}
}
Image::store($upload, 'tour_' . $tour_id, $name);
if ($stored > 0) {
flash_set('status', $stored === 1 ? 'Bild wurde hochgeladen.' : $stored . ' Bilder wurden hochgeladen.');
}
if ($problems !== []) {
flash_set('error', implode(' ', array_unique($problems)));
} elseif ($stored === 0) {
flash_set('error', 'Bitte mindestens eine Bilddatei auswählen.');
}
redirect('/tour/gallery/' . $tour_id);
@ -155,4 +198,22 @@ class GalleryController
mkdir($dir, 0777, true);
}
}
/** true, wenn das Zielverzeichnis (oder sein Elternverzeichnis) beschreibbar ist. */
private function isUploadReady(string $category): bool
{
$dir = storage_path('img_storage/' . Image::slug($category));
if (is_dir($dir)) {
return is_writable($dir);
}
$parent = storage_path('img_storage');
if (is_dir($parent)) {
return is_writable($parent);
}
return is_writable(storage_path());
}
}

View file

@ -178,15 +178,28 @@ class TourController
redirect('/tour');
}
// Bild wird in Image::store() inhaltlich geprüft (Format/Größe);
// die dortige Fehlermeldung wird als Flash-Meldung angezeigt.
validate([
'tour_id' => 'required',
'name' => 'required|min:3',
'user_id' => 'required',
'bild' => 'mimes:jpg,jpeg,bmp,png,gif',
]);
$imageError = null;
if (!empty($_FILES['bild']['name'])) {
Image::store($_FILES['bild'], 'tour_' . $tourId, (string) input('name'));
try {
Image::store($_FILES['bild'], 'tour_' . $tourId, (string) input('name'));
} catch (\Throwable $e) {
if (!$e instanceof \RuntimeException) {
error_log('Tourbild-Upload: ' . $e);
}
$imageError = $e instanceof \RuntimeException
? $e->getMessage()
: 'Das Bild konnte nicht verarbeitet werden.';
}
}
$tour->name = (string) input('name');
@ -214,6 +227,11 @@ class TourController
$tour->updated_at = now();
$tour->save();
if ($imageError !== null) {
flash_set('error', $imageError);
redirect('/tour/' . $tourId . '/edit');
}
redirect('/tour');
}

View file

@ -99,15 +99,28 @@ class UserController
redirect('/member');
}
// Bild wird in Image::store() inhaltlich geprüft (Format/Größe);
// die dortige Fehlermeldung wird als Flash-Meldung angezeigt.
validate([
'name' => 'required|min:3',
'email' => 'required|email',
'mobile' => 'required',
'bild' => 'mimes:jpg,jpeg,bmp,png,gif',
]);
$imageError = null;
if (!empty($_FILES['bild']['name'])) {
Image::store($_FILES['bild'], 'user', (string) $userId);
try {
Image::store($_FILES['bild'], 'user', (string) $userId);
} catch (\Throwable $e) {
if (!$e instanceof \RuntimeException) {
error_log('Benutzerbild-Upload: ' . $e);
}
$imageError = $e instanceof \RuntimeException
? $e->getMessage()
: 'Das Bild konnte nicht verarbeitet werden.';
}
}
$user->name = (string) input('name');
@ -122,6 +135,11 @@ class UserController
$user->updated_at = now();
$user->save();
if ($imageError !== null) {
flash_set('error', $imageError);
redirect('/member/' . $userId . '/edit');
}
redirect('/member');
}