简体   繁体   中英

Upload a Profile Image in PHP & MySQL: What am I doing wrong?

I wrote the code thrice before coming here. Each time I got the same problem: The profile image does not change from the default placeholder to the new uploaded image. Here is the index.php file:

<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "imgupload");
?>
<!DOCTYPE html><html><head></head><body>
<?php
    $sql_user = "SELECT * FROM user";
    $result = mysqli_query($conn, $sql_user);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $id = $row['id']; //gets the id of the user just selected from the database
            $sql_img = "SELECT * FROM profileimg WHERE userid='$id'"; //check if we actually have a profile image uploaded from this user
            $resultImg = mysqli_query($conn, $sql_img); //check what the status is of this user in the image table
            while ($rowImg = mysqli_fetch_assoc($resultImg)) {
                echo "<div class='user-container'>"; //check image status: uploaded or not uploaded
                    if ($rowImg['status'] == 0) {
                        echo "<img src='uploads/profile".$id.".jpg'>";
                    } else {
                        echo "<img src='uploads/profiledefault.jpg'>";
                    }
                    echo "<p>".$row['username']."</p>";
                echo "</div>";
            }
        }
    } else {
        echo "There are no users yet!";
    }

    if (isset($_SESSION['id'])) {
        if ($_SESSION['id'] == 1) {
            echo "You are logged in as user #1";
        }
        echo "<form action='upload.php' method='POST' enctype='multipart/form-data'>
            <input type='file' name='file'>
            <button type='submit' name='submit'>UPLOAD</button>
        </form>";
    } else {
        echo "You are not logged in!";
        echo "<form action='signup.php' method='POST'>
            <input type='text' name='first' placeholder='First name'>
            <input type='text' name='last' placeholder='Last name'>
            <input type='text' name='uid' placeholder='Username'>
            <input type='password' name='pwd' placeholder='Password'>
            <button type='submit' name='submitSignup'>Signup</button>
        </form>";
    }
?>
<p>Login as Admin</p><form action="login.php" method="POST"><button type="submit" name="submitLogin">Login</button></form><p>Logout</p>
<form action="logout.php" method="POST"><button type="submit" name="submitLogout">Logout</button></form></body></html>

And here is the upload.php file:

<?php
session_start();
$conn = mysqli_connect("localhost", "root", "", "imgupload");
$id = $_SESSION['id'];
if (isset($_POST['submit'])) {
    $file = $_FILES['file'];

    $fileName = $file['name'];
    $fileTmpName = $file['tmp_name'];
    $fileSize = $file['size'];
    $fileError = $file['error'];
    $fileType = $file['type'];

    $fileExt = explode('.', $fileName); //returns an array of data
    $fileActualExt = strtolower(end($fileExt)); //returns the last item in the array then lowercases it

    $allowed = array('jpg', 'jpeg', 'png', 'pdf');

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0) {
            if ($fileSize < 1000000) {
                $fileNameNew = "profile".$id.".".$fileActualExt;
                $fileDestination = 'uploads/'.$fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                $sql = "UPDATE profileimg SET status=0 WHERE userid='$id';";
                $result = mysqli_query($conn, $sql);
                header("Location: index.php?uploadsuccess");
            } else {
                echo "Your file is too big!";
            }
        } else {
            echo "There was an error uploading your file!";
        }
    } else {
        echo "You cannot upload files of this type!";
    }
}

I doubt there's anything wrong in the signup.php file. Why won't the profile image change when I upload?

I don't see you set $_SESSION ["id"] . I think $ _SESSION ["id"] duplicate and

$fileNameNew = "profile".$id.".".$fileActualExt;

$fileNameNew can be as "profile1.jpg", if profile1.jpg already exists then you cannot add file profile1.jpg to upload folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM