簡體   English   中英

在PHP和MySQL中上傳個人資料圖片:我做錯了什么?

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

我來這里之前寫了三次代碼。 每次遇到相同的問題時,個人資料圖像都不會從默認占位符更改為新上傳的圖像。 這是index.php文件:

<?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>

這是upload.php文件:

<?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!";
    }
}

我懷疑signup.php文件中是否存在任何錯誤。 為什么上傳我的個人資料圖像時不會更改?

我看不到您設置了$ _SESSION [“ id”]。 我認為$ _SESSION [“ id”]重復且

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

$ fileNameNew可以是“ profile1.jpg”,如果profile1.jpg已經存在,則無法將文件profile1.jpg添加到上傳文件夾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM