更新旧图片后更新旧图片之前删除旧图片之前的图片更新

时间:2019-06-06 08:51:55

标签: php

我正在使用PHP 7.2更新图片表名称横幅和字段ID,图像,发布,更新日期。 我需要在更新新文件后删除oldfile文件,它可能比我有帮助,请在这里共享php代码,数据库和html表格...

表格名称为横幅

  

这样的数据库

id |  image           | posted       | update_date

  1|  amit-1234.jpg   | 05/06/2019   | 06/06/2019

  2|  amit-1254.jpg   | 05/06/2019   | 06/06/2019

  3|  ravi-1236.jpg   | 05/06/2019   | 06/06/2019

  4|  mohan-1274.jpg  | 05/06/2019   | 06/06/2019

更新Pictute PHP脚本

 <?php 

    $msg='';

    if(isset($_POST['update'])){

    $banner_id= mysqli_real_escape_string($conn, $_POST['banner_id']);

    $update_date   = date('d-m-y');

    $sql9 = "SELECT * FROM banner WHERE banner_id='$banner_id'";

    $result9 = mysqli_query($conn, $sql9);

    mysqli_num_rows($result9) > 0;

    $row9 = mysqli_fetch_assoc($result9);

    if(!empty($_FILES['image'] ['name'])){ 

    $extension = explode('.', $_FILES['image'] ['name']);

    $origname= $extension[0];

    $image = $origname . '-' .rand(10000,99999) . '.' . $extension[1];

    $file_loc1 = $_FILES['image'] ['tmp_name'];

    $folder1="vendors/banner/";

    $dir="vendors/banner/"; //error this part is not workiing

    unlink($dir.'/'.$folder1);//error this part is not workiing

    move_uploaded_file($file_loc1,$folder1.$image);


    } else{$image=$row9['image'];}

    $sql = "UPDATE banner SET image='$image',update_date='$update_date' where 
     banner_id='$banner_id'";
    if ($conn->query($sql) === TRUE) {

    $msg="Successfully Updated your Banner Thank You ";


     }

     } 

    ?>

HTML表单

<form action="" method="post" enctype="multipart/form-data">

 <div class="form-group">

    <label >BANNER IMAGE :</label>
    <div><center><img src="vendors/banner/<?php echo $row['image'];?>" style="width:100%;   height:108px;"></center></div><br>

    <input class="form-control form-control-lg p-0" type="file" name="image" value="<?php echo $row['image'];?>" >

</div>
 <button type="submit" name="update" class="btn btn-info">UPDATE BANNER</button>

</form>

0 个答案:

没有答案