我想在登录时显示我的用户个人资料照片。当他们单击change profile picture
按钮时,他们的图片将保存到目录中,然后保存到数据库中。我不知道从哪里开始。如何显示用户拥有的任何图片,作为他们的个人资料图片?
profile.php:
<form id="form2" action="upload.php" method="post" enctype="multipart/form-data">
<p id="p1">Change profile picture:</p> <br />
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<br><input id="sub1" type="submit" value="Change profile picture" name="change"><br />
</form>
<!-- Trigger the Modal -->
<img id="myImg" src="default.jpg" width="200" height="150">
EDIT !!!!
<?php
$username = $_SESSION['username'];
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPG, JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[]='File size must be 2 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"images/uploads/".$file_name);
$store=mysqli_query($conn,"UPDATE users SET userPic='$userPic' WHERE username='$username'");
mysqli_query($conn,$store);
echo "Success";
}else{
print_r($errors);
echo"it failed";
}
}
?>
<?php
$getimg = mysqli_query($conn,"SELECT userPic FROM users WHERE username='" .
$username . "'");
$rows=mysqli_fetch_array($getimg);
$img = $rows['userPic'];
?>
<img id="myImg" src="images/uploads/<?php echo $img?>" alt="<?php echo $img ?>" width="200" height="150">
答案 0 :(得分:1)
实现这一目标:
1)使用With ieDoc.forms(0)
.username.Value = "montemor"
.password.Value = "example"
.submit
在配置文件中定义用户个人资料图片的目录
例如:define()
2)当用户登录时使用sql查询获取图像名称,并使用登录的用户ID /用户名从db获取配置文件图片名称
3)传递define('PROFILE_PATH', 'your dir path');
并在标记
答案 1 :(得分:0)
测试!
<?php
$username = isset($_SESSION['username']) ? $_SESSION['username'] : "";
$conn = mysqli_connect("localhost", "root", "", "test");
if(!empty($username))
{
if(isset($_FILES['image']))
{
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[]='File size must be 2 MB';
}
if(empty($errors)==true)
{
move_uploaded_file($file_tmp,"uploads/".$file_name);
$store = "UPDATE users SET userPic='$file_name' WHERE username='$username'";
if(mysqli_query($conn, $store))
{
echo "Success";
}
else
{
echo "Update failed!";
}
}else{
print_r($errors);
echo"it failed";
}
}
?>
<?php
$getimg = mysqli_query($conn,"SELECT userPic FROM users WHERE
username='$username'");
$rows=mysqli_fetch_array($getimg);
$img = $rows['userPic'];
?>
<img id="myImg" src="images/uploads/<?php echo $img?>" alt="<?php echo $img ?>" width="200" height="150">
<?php
}
else
{
echo "Invalid Username";
}
如果您需要指南,可以给我留言
答案 2 :(得分:0)
好的,所以我的第一个问题是你使用某种数据库吗?如果你在users表中创建一个名为“avatar”的字段,那么我们将存储文件名。其次在你的localhost目录中创建一个名为“uploads”的文件,我们将在这里存储图片的文件名。 其次你有会议吗?如果是这样,我已将会话定义为$ user,因此请将其更改为适合您的会话。然后只需添加此代码即可。我把它全部放在同一个文件中但你可以把它作为一个单独的文件,如果你愿意的话。
<?php
$servername = "localhost";
$username = "";
$password = "";
$database = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
$user = $_SESSION['username'];
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[]='File size must be 2 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"uploads/".$file_name);
$store=mysqli_query($conn,"UPDATE tbl SET avatar='$file_name' WHERE username='$user'");
mysqli_query($conn,$store);
echo "Success";
}else{
print_r($errors);
echo"it failed";
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<p id="p1">Change profile picture:</p> <br />
<input type="file" name="image" />
<input type="submit"/>
</form>
<?php
$getimg = mysqli_query($conn,"SELECT avatar FROM tbl WHERE username='$user'");
$rows=mysqli_fetch_array($getimg);
$img = $rows['avatar'];
?>
<img id="myImg" src="uploads/<?php echo $img?>" alt="<?php echo $img ?>" width="200" height="150">
如果您有任何问题,请随时与我们联系。