首页 > 解决方案 > 更新新图像仍然显示旧图像

问题描述

(我想上传一个与以前同名的新图像。所有代码都运行良好......但是当我尝试获取图像时,它会显示上一个图像)任何解决方案????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();

}
?>

'''

在这里我如何使用取消链接功能????或者任何人都可以删除旧图像并创建和上传具有相同名称的新图像的代码

标签: php

解决方案


如果您需要在 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 中删除现有文件并使用旧名称上传新图像。


推荐阅读