首页 > 解决方案 > 上传后个人资料图片未在浏览器中更新

问题描述

我还在学习PHP。我希望我上传的图片在上传后显示。它正在上传,但不会更改浏览器上上传的图片。我重新启动计算机后它会改变。有时当我更改浏览器时它会改变。我正在使用 Chrome。这里可能是什么问题,因为它正在上传和更新数据库。但是上传的图片在上传时并没有立即改变。这是我的代码。

<?php

include 'config.php';
session_start();

$user=$_SESSION['username'];

$update = $conn->real_escape_string($_POST['update']);
$phone = $conn->real_escape_string($_POST['phone']);
$email = $conn->real_escape_string($_POST['email']);
$pswd = $conn->real_escape_string($_POST['pswd']);
$pswd2 = $conn->real_escape_string($_POST['pswd2']);
//$avatar = $_POST['name'];

if ($pswd!=$pswd2)
{
	$_SESSION["message"] = "Passwords do not match. Please try again.";
	header("location:profile?error");
}

else
{
	$pswd = password_hash($pswd, PASSWORD_BCRYPT);

	$sql= "UPDATE students SET email='$email' WHERE username='$user'";
	$sql2 ="UPDATE students SET phonenum='$phone' WHERE username='$user'";
	$result = $conn->query($sql) and $conn->query($sql2);

if (empty($pswd) || empty($pswd2))
{
	header("location:profile");
}
else
{
		$sql3= "UPDATE students SET pswd='$pswd' WHERE username='$user'";
}
	$passresult=$conn->query($sql3);
		if($result==TRUE && $passresult==TRUE)
		{
		$_SESSION['message'] = "Profile Updated!";
		header("location:profile.php?done");
		}
}
						   						    
if(isset($_POST['submit'])){
 //Targeting Folder
 $target="propics/";
 $target=$target.basename($_FILES['propic']['name']);
 //Getting Selected image Type
 $type=pathinfo($target,PATHINFO_EXTENSION);
 //Allow only image Format To Upload
 if($type!='jpg' && $type!='jpeg' && $type!='JPG' && $type!='PNG' && $type!='GIF'  && $type!='png' && $type!='gif'){
  echo "Only JPG,JPEG,PNG and GIF file format are allowed to Upload";
 }
 else{
 	//lmit file size
 	if ($_FILES['propic']['size'] > 5000000) {
        echo "Sorry, your image is too large.";        
        }
 
    else{
   
   
  $upload_success=move_uploaded_file($_FILES['propic']['tmp_name'],$target);
  if($upload_success==TRUE){
   //Getting Selected image Information

    //renaming the file to the username to avoid conflict when uploading
    $name=$user.".".$type;
    rename("propics/".$_FILES['propic']['name'],"propics/".$name);	
    //					 
    $sql= "UPDATE students SET propic='$name' WHERE username='$user'";
   $result = $conn->query($sql);
   if($result==TRUE){
	$_SESSION['message']= "Profile picture updated successfully!";
   header("location:/prac/profile.php?done");
   }
  }
  }
  
 }
}
?>

下面是显示图片的代码。

		<img id="mainpropic" src="propics/<?php echo $row['propic'];?>" width="200px" height="200px" >

标签: php

解决方案


我意识到这是一个浏览器问题,浏览器正在缓存图片。如果我在没有缓存的情况下刷新一切正常。


推荐阅读