php - 更新新图像仍然显示旧图像
问题描述
(我想上传一个与以前同名的新图像。所有代码都运行良好......但是当我尝试获取图像时,它会显示上一个图像)任何解决方案????mysql表
我的代码
<?php
include "nav.php";
$id = $_GET['id'];
$id = base64_decode($id);
$q = "select * from slider where id =".$id;
include "js.php";
include "db.php";
$r = mysqli_query($con,$q);
$row = mysqli_fetch_assoc($r);
?>
<div style = "width:70%;margin-left:13%;">
<form method = "post" enctype="multipart/form-data">
<br>
<center>
<input type = "text" value="<?php echo $row["text"];?>" name = "title" placeholder="Title" style = "width:100%;" required>
<br>
<br>
<input type="file" name = "m" class="form-control-file" accept="image/x-png,image/gif,image/jpeg" id="exampleFormControlFile1" required>
<input class = "btn btn-primary" type = "submit" name = "submit" value="Submit">
</center>
</form>
</div>
<?php
if(isset($_POST["submit"])){
$t =$_POST['title'];
$allow = array('jpg');
$temp =explode(".",$_FILES['m']['name']);
$extension= end($temp);
$newfilename=$row['id'] .".".$extension;
if($extension=="jpg")
{
$upload_file=$newfilename;
move_uploaded_file($_FILES['m']['tmp_name'] , "slider/".$upload_file);
}
else
{
echo "<script>alert('Image should be in jpg');</script>";
exit();
}
$q = "update slider set text= '".$t."' where id= ".$row['id'];
$r = mysqli_query($con,$q);
header('location:slider.php');
exit();
}
?>
'''
在这里我如何使用取消链接功能????或者任何人都可以删除旧图像并创建和上传具有相同名称的新图像的代码
解决方案
如果您需要在 ftp 中更新图像但在数据库中使用相同的名称,请首先从数据库中获取该行的数据并将图像的值存储在如下变量中:
// write your sql query here
$upload_file=$old_file_name; // value should be fetch from database.
然后使用以下命令从 ftp 取消链接图像:
unlink('/path/imagename.png'); // set your path and change the name and extension
取消链接图像后,上传新图像并在上传时删除。因此它可以从 ftp 中删除现有文件并使用旧名称上传新图像。
推荐阅读
- python - Sklearn 和 StatsModels 给出了非常不同的逻辑回归答案
- html - 忽略点击图片的透明部分
- c# - C# WCF 服务,阅读soapenv 标头安全部分
- javascript - 模拟对课堂的休息反应
- python - 我如何在python中循环打印
- c# - Blazor NavMenu 没有默认使用 @page "/" 和 querystring
- node.js - 为什么 heroku post-build 将 `/app` 附加到我的启动脚本文件?
- angular - tsconfig.app.json 的别名不起作用 Angular 11
- java - 比较两个相同的 UUID 时,为什么布尔值是假的?
- r - 图例未显示在散点图中