我有一段代码可以上传图片并将其保存在文件夹和数据库中的路径中并在网页上显示。有趣的是,在第一次上传图片时,图片将显示在网页上,并在我上传新图片时更改。但是当我关闭页面,再次打开它并决定更改图片时,即使刷新页面,网页中的一个也不会改变,但文件夹中的那个会发生变化。
这是我的代码
<?php
$sql2 = "SELECT Picture_HD FROM detailss WHERE Idn_nom = '$Indnum'";
require('connect.php');
$addr = "";
$addr = mysqli_query($conn, $sql2);
if ($addr) {
$locat = $addr->fetch_row();
$locat = (string)$locat[0];
} else {
$locat = "Pictures/default1.png";
}
mysqli_close($conn);
echo "<div id = 'Img'>";
echo "<img src = '" . $locat . "' alt = 'Passport picture/Headshot' style = 'width:80px; height:80px;'/>";
echo "</div>";
?>
答案 0 :(得分:0)
如果您的图片在您的文件夹中发生了变化,但是您在网页上看到旧图片可能是一个缓存问题,请清除您的浏览器缓存(ctrl + f5加上这有点不对,所以不会这样做总是工作 - 所以最好进入浏览器设置,或者在ctrl + f5之后打开一个私人窗口,如果不是清除服务器级缓存。
答案 1 :(得分:0)
您的浏览器正在缓存图片。 如果您想阻止浏览器缓存图像,只需在网址末尾添加一个随机参数。
echo "<img src = '" . $locat . "?t=" . time() . "' alt = 'Passport picture/Headshot' style = 'width:80px; height:80px;'/>";
答案 2 :(得分:0)
最好的方法是在上传具有相同文件名的新图像之前删除现有图像
// 定义用于文件名的变量来自会话变量 username 目录和扩展名,方法是从具有元数据类型的表单中分解来自 post 方法的文件名
//set new file name to username from session variable
$filename = $_SESSION['username']
// set directory of files
$dir = "img/";
// set extension variable to file extension after posted from form
$ext=strtolower(end(explode('.',$_FILES['importimg']['name'])));
// new file upload name with existing extension
$upload_file = $dir . $filename . "." . $ext;
// delete file
// find all files with the same name any extension using variable defined above etc .txt, .php, .gif, .jpg, etc. then delete it
foreach (glob("img/$filename.*") as $deletefile) {
// unlink is used to delete the file and delete the cache of the file
unlink($deletefile);
}
// upload image
// upload file with type posted from metadata in form and upload it as your new file name using upload_file variable
if (move_uploaded_file($_FILES['importimg']['tmp_name'], $upload_file)) {
// successful upload of file add code for msg or sql query etc name to users table and redirect to profile page
echo "Successfully uploaded your file.";
} else {
// upload error show message
echo "There was an error uploading your file.";
}
享受