简体   繁体   English

如何获取从用户到我的数据库到特定登录用户的上传文件?

[英]How can I get an upload file from user to my database to specific user logged in?

I have a feature below which is an upload picture feature which allows users to upload a picture of themselves. 我下面有一个功能,它是一个上传图片功能,允许用户上传自己的图片。 It is meant to upload the picture to a directory, and then store in a database (whichever user is logged in) and also should display for the user. 它旨在将图片上传到目录,然后存储在数据库中(无论哪个用户登录),并且还应为用户显示。 What seems to be happening for me is that it is inserting into the database as an 'Array' but not sure who I can get it to link to UserID I have logged in? 对我来说似乎正在发生的事情是,它以“数组”的形式插入数据库中,但不确定是否可以将其链接到我登录的UserID上?

See code below: 参见下面的代码:

error_reporting(E_ALL);
ini_set('display_errors', 1);
include("dbConnect.php");

if(isset($_POST['submit'])) {
    $file = $_FILES['file'];

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

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array(
        'jpg',
        'jpeg',
        'png',
        'pdf'
    );
    if(in_array($fileActualExt, $allowed)) {
        if($fileError === 0) {
            if($fileSize < 1000000) {
                $fileNameNew = uniqid('', true) . "." . $fileActualExt;
                $fileDestination = 'uploads/' . $fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                header("Location: myProfile.php?successCode=1");
                echo "<img src=" . $fileDestination . " height=200 width=300/>";
            } else {
                echo "your file too big";
            }
        } else {
            echo "There was error uploading file";
        }
    } else {
        echo "you cant upload files of this type";
    }
    // Insert record
    $stmt = $conn->prepare("INSERT into Profile(ProfilePicture) values('" . $file . "')");
    $stmt->execute();
}

The problem is you are saving an array to your database. 问题是您正在将数组保存到数据库中。 You cannot just save a picture to your database, but you CAN save the PATH to your image. 您不仅可以将图片保存到数据库中,还可以将PATH保存到图片中。

$stmt = $conn->prepare("UPDATE Profile SET ProfilePicture='{$fileName}' WHERE UserID='{$_SESSION['currentUserID']}'");

The above will save just the name of the file, so if your image directory never changes, this should be good enough and you can reference the actual directory in your code elsewhere. 上面的代码仅保存文件名,因此,如果您的图像目录永不更改,这将足够好,您可以在代码中的其他位置引用实际目录。

-- "I want the code to insert the ProfilePicture where UserID=UserID logged in?" - “我要代码在其中插入UserID = UserID的ProfilePicture吗?” - In regards to this from the comments, I updated my answer to show you how to UPDATE the table where UserID='$_SESSION['currentUserID']' -关于评论的内容,我更新了答案,向您展示了如何更新其中UserID='$_SESSION['currentUserID']'

NOTE: You should consider switching to prepared statements with parameterized variables , as the method currently in use may be vulnerable to SQL Injection . 注意:您应该考虑切换到带有参数化变量的 预处理语句 ,因为当前使用的方法可能容易受到SQL Injection的攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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