允许用户在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.
}