Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
允许用户在PHP中上载配置文件图像_Php_Mysql_Image_File Upload - Fatal编程技术网

允许用户在PHP中上载配置文件图像

允许用户在PHP中上载配置文件图像,php,mysql,image,file-upload,Php,Mysql,Image,File Upload,我已经有了一个合适的图像上传系统。它上载并将图像存储在文件夹/uploads/,现在我需要跟踪谁上载了配置文件图像,我想在我的用户配置文件页面上显示该图像 这是我的上传。php: <?php include('db.php'); // Check if user is logged in using the session variable if ( $_SESSION['logged_in'] != 1 ) { $_SESSION['message'] = "You must lo

我已经有了一个合适的图像上传系统。它上载并将图像存储在文件夹
/uploads/
,现在我需要跟踪谁上载了配置文件图像,我想在我的用户配置文件页面上显示该图像

这是我的上传。php:

<?php
include('db.php');

// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
  $_SESSION['message'] = "You must log in before viewing your profile page!";
  header("location: error.php");    
}

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);
    $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: user.php");
            } 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!";
    }
}

您不需要为此使用其他表。只需在第一个表中再添加一列“profile_image”,并将图像保存在该表中

 if (move_uploaded_file($fileTmpName, $fileDestination)) {
 // save/update "profile_image" field here.
}

当您想显示配置文件图像时,只需检查“配置文件图像”列是否为空。如果是show default image“/uploads/profiledefault.jpg”,则从“profile\u image”列显示配置文件图像。

我相信您有名为User的实体。在此实体中,添加属性
profileImage
,并在上传图像后保存图像路径。获取当前用户并将文件路径添加到其属性
profileImage
。每次注册新用户时,您只需给默认图像的路径
profileImage
/uploads/profiledefault.jpg
。这意味着用户在每一点上都有profileImage,无需检查它

我猜我将不得不使用UPDATE查询来更新profile_image列,WHERE子句将包含每个用户的唯一值,例如他/她的电子邮件?
 if (move_uploaded_file($fileTmpName, $fileDestination)) {
 // save/update "profile_image" field here.
}