search

Home  >  Q&A  >  body text

Image uploaded successfully

<p>My image shows up as uploaded in the URL which means it has been uploaded correctly but I'm not sure as I don't see it appearing in the webpage or database but it does appear in my folder directory . </p> <p>Here are the two pages I use to do this process. </p> <p>account.php is where I upload pictures, and profile-upload.php is the server/backend of account.php. </p> <p>My account.php:</p> <pre class="brush:php;toolbar:false;"><?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"; ?></pre> <p>这是profile-upload.php:</p> <pre class="brush:php;toolbar:false;"><?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(); }</pre> <p>我尝试编辑代码并刷新数据库,但我没有看到问题。它已连接到数据库,CSS也是正确的。</p>
P粉585541766P粉585541766473 days ago585

reply all(1)I'll reply

  • P粉825079798

    P粉8250797982023-08-16 09:31:31

    I think I know where the problem is. In your account.php file you are trying to display the image with the following code:

    echo '<img src="' . $row['image'] . '">';

    To fix this problem, you need to change the src attribute to the full URL of the image file. You can use the realpath() function in PHP to get the complete URL of the image file. For example, the following code will get the full URL of the image file and display it on the page:

    $imageUrl = realpath('../uploads/' . $row['image']);
    echo '<img src="' . $imageUrl . '">';

    reply
    0
  • Cancelreply