如何在上传后永久显示图片?
问题描述:
Hi
我在下面有一个功能,允许用户为自己上传个人资料图片,存储在目录和数据库中,链接到他们的USERID。一旦他们上传,我想显示这个图像,并显示他们是否离开它然后重新进入?
有人可以帮我解决这个问题吗?
谢谢
我的尝试:
Hi
I have a feature below in which allows user to upload a profile picture for themselves, Store in directory and in the database which links to their USERID. Once they upload, i want to display this image and also display if they go out of it and then back in?
Can someone help me out with code for this?
Thanks
What I have tried:
<?php
session_start();
include("dbConnect.php");
if(isset($_SESSION ['currentUserID'])){
echo "<h1> Upload an image of yourself here!</h1>
<form action='myProfile.php' method='POST' enctype='multipart/form-data'>
<input type='file' name='file'>
<br><br><button type='submit' name='submit'>Upload photo</button>
</form>";
}
error_reporting(E_ALL);
ini_set('display_errors', 1);
$id =$_SESSION['currentUserID'];
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 = "profile".$id.".".$fileActualExt;
$fileDestination = 'uploads/'. $fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
header("Location: myProfile.php?successCode=1");
echo "<img src='uploads/profile".$id.".jpg'>";
}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("UPDATE Profile SET ProfilePicture='{$fileName}' WHERE UserID='{$_SESSION ['currentUserID']}'");
$stmt->execute();
}
//SuccessMessage
if (isset($_GET["successCode"])) {
if ($_GET["successCode"]==1)
echo "<h3>Profile Picture uploaded!</h3>";
}
?>
答
_SESSION ['currentUserID'])){
echo < h1>在此上传您自己的图片!< / h1>
< form action ='myProfile.php'methode ='POST'enctype ='multipart / form-data'>
< input type ='file'name ='file'>
< br>< br>< button type ='submit'name ='submit'>上传照片< / button>
< / form>;
}
error_reporting(E_ALL);
ini_set('display_errors',1);
_SESSION ['currentUserID'])){ echo "<h1> Upload an image of yourself here!</h1> <form action='myProfile.php' method='POST' enctype='multipart/form-data'> <input type='file' name='file'> <br><br><button type='submit' name='submit'>Upload photo</button> </form>"; } error_reporting(E_ALL); ini_set('display_errors', 1);
id =
_SESSION ['currentUserID'];
if(isset(
_SESSION['currentUserID']; if (isset(