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:52:22 +01:00
parent 81724c62b0
commit f98881ce97

View File

@ -64,9 +64,18 @@ $ext = $allowedMimeToExt[$mime];
// Für move_uploaded_file() brauchen wir einen Dateisystempfad; die Public-URL ist separat.
$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 . 'assets/images/profilePictures'
: __DIR__ . 'assets/images/profilePictures';
? $webRoot . $relativeTargetDir
: rtrim(__DIR__, "\\/") . $relativeTargetDir;
error_log('Upload: resolved targetDir=' . $targetDir);
if (!is_dir($targetDir))
{
@ -91,6 +100,7 @@ $rand = uniqid('', true);
$rand = str_replace('.', '', $rand);
$filename = 'user_' . $userId . '_' . $rand . '.' . $ext;
$targetPath = rtrim($targetDir, "\\/") . DIRECTORY_SEPARATOR . $filename;
error_log('Upload: resolved targetPath=' . $targetPath);
if (!move_uploaded_file($tmp, $targetPath))
{