Mohammad Taqi
Mohammad Taqi

Reputation: 179

Updating new image still showing the old image

(I wanted to upload a new image with the same name as previous. All code is working well... but when i try to fetch image it show the previous image) Any Solution???? Mysql Table Here is the table

My Code

<?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();

}
?>

'''

Here how can i use unlink funtion???? or can any one me the code to delete the old image and create and upload a new image with the same name

Upvotes: 0

Views: 2732

Answers (2)

Mohammad Taqi
Mohammad Taqi

Reputation: 179

here is the solution ..... (It will delet the old pic and upload the new one in folder....)

  <?php
//delet the previous img
                    unlink("slider/" . $oldimage);

          // If upload button is clicked ...
          if (isset($_POST['submit'])) {
            // Get image name
            $title=$_POST['title'];
            $filename = $_FILES['image']['name'];
        $filetmpname = $_FILES['image']['tmp_name'];
        //folder where images will be uploaded
        $folder = 'slider/';
        //function for saving the uploaded images in a specific folder
        move_uploaded_file($filetmpname, $folder.$filename);
        //inserting image details (ie image name) in the database
        $sql = "UPDATE slider SET image='$filename',text='$title' where id=$id";
        $qry = mysqli_query($con, $sql);
        if( $qry) {
        echo "</br>image uploaded";
        }header('Location:slider.php');
          }
        ?>

got uploading idea from here https://medium.com/@mauricemuteti2015/how-to-upload-and-insert-image-into-mysql-database-using-php-and-html-32633a06d372

Upvotes: 0

nikhil borikar
nikhil borikar

Reputation: 60

if you need to update the image in ftp but with the same name in database, first fetch the data from the database for that row and store the value of the image in a variable like this :

     // write your sql query here
    $upload_file=$old_file_name;  // value should be fetch from database.

then unlink the image from the ftp using:

unlink('/path/imagename.png'); // set your path and change the name and extension

once you unlink the image upload the new image and remove while uploading it. so it can remove existing file from ftp and upload new image with old name.

Upvotes: 1

Related Questions