我的图片在URL中写着已上传,这意味着它已正确上传,但我不确定,因为我没有看到它出现在我的网页或数据库中,但它出现在我的文件夹目录中.
这是我用来完成这一过程的两页.
Php是我上载图像的位置,而profile-pload.php是ackt.php的服务器端/后端.
我的帐户.php:
<?php
session_start();
include_once "safe-header.php";
include_once "serverside/database-server.php";
if (isset($_SESSION['useremail']) && !empty($_SESSION['useremail'])) {
$useremail = $_SESSION['useremail'];
} else {
$useremail = 'User';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="Css/account.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alternative Account Page</title>
</head>
<body>
<div class="upload-container">
<p class="useremail">Welcome, <?php echo htmlspecialchars($useremail); ?></p>
<div class="account-options">
<a href="update-profile.php" class="update-profile">Update Profile</a>
<a href="serverside/logout-server.php" class="logout-btn">Logout</a>
</div>
<div>
<?php
$userId = $_SESSION['userid'] ?? null;
$sql = "SELECT image FROM profile WHERE usersId = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if ($row && !empty($row['image'])) {
echo '<img src="' . $row['image'] . '">';
} else {
echo "<p>Nothing here.</p>";
}
?>
<form action="serverside/profile-upload.php" method="post" enctype="multipart/form-data">
<label for="profile-image">Select Image</label>
<input type="file" name="profile-image">
<button type="submit" name="upload-btn">Upload Image</button>
</form>
</div>
</div>
<?php
include_once "footer.php";
?>
以下是配置文件-pload.php:
<?php
session_start();
require "database-server.php";
if (isset($_POST['upload-btn'])) {
$validExt = ['jpg', 'jpeg', 'png'];
$ext = strtolower(pathinfo($_FILES['profile-image']['name'], PATHINFO_EXTENSION));
if (in_array($ext, $validExt)) {
$location = "../uploads/";
$target = $location . uniqid() . '.' . $ext;
if (move_uploaded_file($_FILES['profile-image']['tmp_name'], $target)) {
$userId = $_SESSION['userid'];
$sql = "UPDATE profile SET image = ? WHERE usersId = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("si", $target, $userId);
$stmt->execute();
$stmt->close();
header("location: ../account.php?success=imageuploaded");
exit();
} else {
header("location: ../account.php?error=imagecantbeuploaded");
exit();
}
} else {
header("location: ../account.php?error=cannotusethisfiletype");
exit();
}
} else {
header("location: ../account.php");
exit();
}
我try 编辑代码并刷新数据库,但没有发现问题.它已连接到数据库,并且css也是正确的.