Refactor profile picture upload path handling to ensure correct directory structure and improve logging

This commit is contained in:
Fabian Schieder 2026-01-25 22:54:05 +01:00
parent f98881ce97
commit 0ba0c9c623

View File

@ -31,6 +31,7 @@ $file = $_FILES['uploadFile'];
$fileError = isset($file['error']) ? (int)$file['error'] : UPLOAD_ERR_NO_FILE;
if ($fileError !== UPLOAD_ERR_OK)
{
error_log('Upload: PHP upload error code=' . $fileError);
header('Location: account.php?upload=err');
exit();
}
@ -65,17 +66,30 @@ $ext = $allowedMimeToExt[$mime];
$documentRoot = isset($_SERVER['DOCUMENT_ROOT']) ? (string)$_SERVER['DOCUMENT_ROOT'] : '';
$webRoot = rtrim($documentRoot, "\\/");
// Wichtig: Beim Zusammenbauen muss ein '/' zwischen Webroot und relativem Pfad stehen.
// Sonst wird z.B. "/FSST/Website" + "assets/..." zu "/FSST/Websiteassets/...".
$relativeTargetDir = '/assets/images/profilePictures';
// Primär: Wenn DOCUMENT_ROOT sauber gesetzt ist, nutze es.
// Fallback: nutze __DIR__ (wenn upload.php im Webroot liegt).
$targetDir = $webRoot !== ''
? $webRoot . $relativeTargetDir
: rtrim(__DIR__, "\\/") . $relativeTargetDir;
// Robust: __DIR__ ist der Ordner, in dem upload.php liegt (bei dir: WEBROOT).
// DOCUMENT_ROOT kann auf manchen Setups leer oder z.B. nur '/' sein; dann würde in '/assets/...' geschrieben.
$dirBasedRoot = rtrim(__DIR__, "\\/");
error_log('Upload: resolved targetDir=' . $targetDir);
define('UPLOAD_DEBUG', true);
$candidateFromDocRoot = ($webRoot !== '' && $webRoot !== '/') ? ($webRoot . $relativeTargetDir) : '';
$candidateFromDir = $dirBasedRoot . $relativeTargetDir;
// Bevorzugt __DIR__; nur wenn DOCUMENT_ROOT plausibel ist und der DIR-Fallback nicht existiert, nutzen wir DOCUMENT_ROOT.
$targetDir = $candidateFromDir;
if ($candidateFromDocRoot !== '' && !is_dir($candidateFromDir) && is_dir($candidateFromDocRoot))
{
$targetDir = $candidateFromDocRoot;
}
if (UPLOAD_DEBUG)
{
error_log('Upload: DOCUMENT_ROOT=' . $documentRoot);
error_log('Upload: __DIR__=' . __DIR__);
error_log('Upload: targetDir=' . $targetDir);
}
if (!is_dir($targetDir))
{