上传文件至 /
This commit is contained in:
167
profile.php
Normal file
167
profile.php
Normal file
@@ -0,0 +1,167 @@
|
||||
<?php
|
||||
// 个人资料:头像上传 / 站点显示名。
|
||||
// 注意区分三个名字:username 登录用(仅后台可见)、nick_name 昵称(管理员可改)、
|
||||
// display_name 站点展示名(用户自己定,手机广场等前台优先用它)。
|
||||
require_once __DIR__ . '/includes/layout.php';
|
||||
require_once __DIR__ . '/includes/upload_guard.php';
|
||||
|
||||
$user = require_password_ready($conn, require_login($conn));
|
||||
|
||||
$message = null;
|
||||
$ok = false;
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['profile_action'])) {
|
||||
if (!csrf_ok()) {
|
||||
$message = '请求校验失败,请刷新页面重试。';
|
||||
} else {
|
||||
$action = $_POST['profile_action'];
|
||||
|
||||
if ($action === 'display_name') {
|
||||
$name = trim($_POST['display_name'] ?? '');
|
||||
if ($name !== '' && !preg_match('/^[\x{4e00}-\x{9fa5}A-Za-z0-9]{1,20}$/u', $name)) {
|
||||
$message = '显示名 1–20 位,只能包含中文、英文和数字。';
|
||||
} else {
|
||||
$stmt = $conn->prepare("UPDATE photo_users SET display_name = ? WHERE user_id = ?");
|
||||
$stmt->bind_param("si", $name, $user['user_id']);
|
||||
$ok = $stmt->execute();
|
||||
$stmt->close();
|
||||
$message = $ok ? ($name === '' ? '已恢复使用昵称作为显示名。' : '显示名已更新。') : '保存失败,请稍后再试。';
|
||||
}
|
||||
} elseif ($action === 'avatar') {
|
||||
$MAX_BYTES = 4 * 1024 * 1024;
|
||||
$res = decode_uploaded_image($_FILES['avatar'] ?? null, $MAX_BYTES);
|
||||
if (!$res['ok']) {
|
||||
$message = $res['message'];
|
||||
} else {
|
||||
$dst = gd_fit($res['src'], 256, true); // 居中裁方形 → 256px
|
||||
$dir = __DIR__ . '/uploads/avatars';
|
||||
if (!is_dir($dir) && !@mkdir($dir, 0755, true)) {
|
||||
imagedestroy($dst);
|
||||
$message = '头像目录不可写,请联系管理员。';
|
||||
} else {
|
||||
$rel = 'uploads/avatars/u' . (int)$user['user_id'] . '_' . bin2hex(random_bytes(4)) . '.png';
|
||||
$abs = __DIR__ . '/' . $rel;
|
||||
$saved = imagepng($dst, $abs, 6);
|
||||
imagedestroy($dst);
|
||||
if (!$saved) {
|
||||
$message = '头像保存失败,请稍后再试。';
|
||||
} else {
|
||||
$old = $user['avatar'] ?? null;
|
||||
$stmt = $conn->prepare("UPDATE photo_users SET avatar = ? WHERE user_id = ?");
|
||||
$stmt->bind_param("si", $rel, $user['user_id']);
|
||||
$ok = $stmt->execute();
|
||||
$stmt->close();
|
||||
if ($ok) {
|
||||
// 只有确认新头像已入库才删旧文件
|
||||
$uploads_dir = realpath(__DIR__ . '/uploads');
|
||||
$full = $old ? realpath(__DIR__ . '/' . $old) : false;
|
||||
if ($full && $uploads_dir && strpos($full, $uploads_dir) === 0 && is_file($full)) {
|
||||
@unlink($full);
|
||||
}
|
||||
$message = '头像已更新。';
|
||||
} else {
|
||||
@unlink($abs);
|
||||
$message = '头像保存失败,请稍后再试。';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif ($action === 'remove_avatar') {
|
||||
$old = $user['avatar'] ?? null;
|
||||
$stmt = $conn->prepare("UPDATE photo_users SET avatar = NULL WHERE user_id = ?");
|
||||
$stmt->bind_param("i", $user['user_id']);
|
||||
$ok = $stmt->execute();
|
||||
$stmt->close();
|
||||
if ($ok && $old) {
|
||||
$uploads_dir = realpath(__DIR__ . '/uploads');
|
||||
$full = realpath(__DIR__ . '/' . $old);
|
||||
if ($full && $uploads_dir && strpos($full, $uploads_dir) === 0 && is_file($full)) {
|
||||
@unlink($full);
|
||||
}
|
||||
}
|
||||
$message = $ok ? '已改回默认头像。' : '操作失败。';
|
||||
}
|
||||
|
||||
if ($ok) {
|
||||
$user = current_user($conn); // 重新取一次,页面立刻反映新值
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
render_header('个人资料', $user, 'profile.php', '头像与站点显示名');
|
||||
?>
|
||||
<div class="page-head">
|
||||
<div>
|
||||
<div class="page-head__title">个人资料</div>
|
||||
<div class="page-head__desc">这些信息会出现在照片广场等公开页面上</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if ($message): ?>
|
||||
<div class="alert alert--<?php echo $ok ? 'success' : 'danger'; ?>" style="max-width:720px">
|
||||
<?php echo icon($ok ? 'check' : 'warn'); ?><span><?php echo h($message); ?></span>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="card" style="max-width:720px">
|
||||
<div class="card__head"><span class="card__title">头像</span><span class="muted small">支持 JPG / PNG / GIF / WEBP,最大 4MB,自动裁成方形 256px</span></div>
|
||||
<div class="card__body">
|
||||
<div class="avatar-edit">
|
||||
<div class="avatar-edit__preview"><?php echo avatar_html($user, 88); ?></div>
|
||||
<div style="flex:1;min-width:220px">
|
||||
<form method="post" enctype="multipart/form-data" id="avatarForm">
|
||||
<input type="hidden" name="csrf_token" value="<?php echo h(csrf_token()); ?>">
|
||||
<input type="hidden" name="profile_action" value="avatar">
|
||||
<input type="file" name="avatar" id="avatarInput" accept="image/*" hidden required>
|
||||
<div class="toolbar" style="margin:0">
|
||||
<button type="button" class="btn btn--primary" id="pickAvatar"><?php echo icon('upload', 15); ?>选择图片</button>
|
||||
<span class="muted small" id="avatarName">未选择文件</span>
|
||||
</div>
|
||||
</form>
|
||||
<?php if (!empty($user['avatar'])): ?>
|
||||
<form method="post" style="margin-top:.625rem">
|
||||
<input type="hidden" name="csrf_token" value="<?php echo h(csrf_token()); ?>">
|
||||
<input type="hidden" name="profile_action" value="remove_avatar">
|
||||
<button class="btn btn--ghost btn--sm" type="submit"><?php echo icon('trash', 14); ?>移除头像,用默认</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="max-width:720px;margin-top:1.25rem">
|
||||
<div class="card__head"><span class="card__title">站点显示名</span></div>
|
||||
<div class="card__body">
|
||||
<form method="post">
|
||||
<input type="hidden" name="csrf_token" value="<?php echo h(csrf_token()); ?>">
|
||||
<input type="hidden" name="profile_action" value="display_name">
|
||||
<div class="field">
|
||||
<label class="label" for="display_name">显示名</label>
|
||||
<input class="input" type="text" id="display_name" name="display_name" maxlength="20"
|
||||
value="<?php echo h($user['display_name']); ?>"
|
||||
placeholder="<?php echo h($user['nick_name']); ?>" style="max-width:320px">
|
||||
<div class="help">
|
||||
留空则使用昵称「<?php echo h($user['nick_name']); ?>」。
|
||||
<strong>登录名 <?php echo h($user['username']); ?> 不会出现在任何公开页面</strong>,仅你自己和后台管理员可见。
|
||||
</div>
|
||||
</div>
|
||||
<button type="submit" class="btn btn--primary"><?php echo icon('check', 15); ?>保存显示名</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.getElementById('pickAvatar').addEventListener('click', function () {
|
||||
document.getElementById('avatarInput').click();
|
||||
});
|
||||
document.getElementById('avatarInput').addEventListener('change', function () {
|
||||
if (!this.files.length) return;
|
||||
var f = this.files[0];
|
||||
if (!f.type.match('image.*')) { toast('请选择图片文件', 'warning'); this.value = ''; return; }
|
||||
if (f.size > 4 * 1024 * 1024) { toast('头像不能超过 4MB', 'warning'); this.value = ''; return; }
|
||||
document.getElementById('avatarName').textContent = f.name;
|
||||
document.getElementById('avatarForm').submit();
|
||||
});
|
||||
</script>
|
||||
<?php render_footer($user, 'profile.php');
|
||||
Reference in New Issue
Block a user