0

I'm showing data from mysql database in Table with below code

<td><?php echo $row['name']; ?></td>
   <td><?php echo $row['image']; ?></td>
   <a title="Delete" href="deletestudent.php?id=<?php echo $row['id']; ?>"><button class="btn btn-danger btn-mini"> Delete</button></a></td>
first rows shows the name of image and second shows image which is inside directory called images.

I am able to delete the row with below code

<?php
 include('../connect.php');
 $id=$_GET['id'];
 $result = $db->prepare("DELETE FROM student WHERE id= :memid");
 $result->bindParam(':memid', $id);
 $result->execute();
 
 header ("location: students.php");
?>
But how can I also delete image from Images folder.

Thanks in advance.

redface
  • 305
  • 1
  • 6
  • 18
  • Are you just asking how to delete a file in PHP? When you did a Google search for "delete a file in PHP" what did you find? Did you try to apply knowledge from the examples found? What didn't work? – David Mar 10 '19 at 19:26
  • I found unlink will work but I couldn't merge both code that when on click delete both image and data at once...I know the image should be deleted before deleting row....but I couldn't know how to apply unlink here....!! – redface Mar 10 '19 at 19:31
  • Possible duplicate of [delete image from folder PHP](https://stackoverflow.com/questions/15005899/delete-image-from-folder-php) – CampbeII Mar 10 '19 at 19:36

3 Answers3

3

Before deleting the image from database you should first delet the image then if its successful delete the image from db.

you can delete the image with php's unlink function. before that make sure the file exists:

<?php
    include('../connect.php');
    $id=$_GET['id'];

       // you must first retrieve data from database to get the image path that 
       //you have been saved in db

   $stmt = $dbh->prepare("SELECT * FROM mytable WHERE id= :memid LIMIT 1"); 
   $stmt->bindParam(':memid', $id);
   $stmt->execute(); 

   $record = $stmt->fetch();

  //get image path
  $imageUrl = $_DIR_.'/images/uploads/profile/'.$record['Image_name'];

  //check if image exists
  if(file_exists($imageUrl)){

    //delete the image
    unlink($imageUrl);

    //after deleting image you can delete the record
    $result = $db->prepare("DELETE FROM student WHERE id= :memid");
    $result->bindParam(':memid', $id);
    $result->execute();
    }
    header ("location: students.php");
?>
Salar Bahador
  • 1,433
  • 1
  • 14
  • 26
  • I have images inside folder called images ..where should i specity path in above code? I believe 'example/url' must be replaced by the value I pass onclick event which is image name...but where is path I should declare for? – redface Mar 10 '19 at 19:36
  • @redface the path that you save in your db to display image – Salar Bahador Mar 10 '19 at 19:43
  • In database under image column I only have image name like 1.png or 2.jpg but images are saved in another folder called images. – redface Mar 10 '19 at 19:45
  • @redface where is the folders location? address it from your current file – Salar Bahador Mar 10 '19 at 19:46
  • @redface is it _DIR_.'/images' ? – Salar Bahador Mar 10 '19 at 19:47
  • If images is in the same directory of your script, you can use images/1.png – Ajanyan Pradeep Mar 10 '19 at 19:47
  • @redface so you must concatenate it with the directory path. don't you know how to address a file? – Salar Bahador Mar 10 '19 at 19:49
  • @redface I edited my answer take a look at the concatenation process – Salar Bahador Mar 10 '19 at 19:51
  • Ii hope it work..I'll test and let you know.....can you tell me how should I pass image name is it same as $id=$_GET['id']; and on click should I pass both id and image and get in delete.php as $image=$_GET['image'] and use $image instead of image_name? – redface Mar 10 '19 at 19:54
  • @redface improvise my friend. open your mind. don't expect to every one guide you on every step. you asked for image deletion so the answer contains the function and the way you can do it. but if you don't know how to address a file research and find your answer elsewhere. the answer is you must pass the image address from your current PHP file. after that, it's up to you to figure it out. – Salar Bahador Mar 10 '19 at 19:59
  • @SalarBahador Thanks ...I'll work accordingly...!! Thanks for your help. – redface Mar 10 '19 at 19:59
  • Is there a rationale for deleting the file first? –  Mar 10 '19 at 23:28
  • 1
    @Chipster : Yes, because once you delete the record, if image deletion is somehow going wrong, this way you wouldn't lose the image path. if you delete the record first and image deletion afterwards, if file deletion process goes wrong you are not able to find the image path – Salar Bahador Mar 11 '19 at 06:26
0

You can delete the image from the file system using:

unlink('path/to/file.jpg');
CampbeII
  • 83
  • 3
  • 11
  • I retrive values with field 'name' from database..how do I apply or what code should I write along with above to apply your solution. – redface Mar 10 '19 at 19:32
  • Please read about relative and absolute pathnames. https://en.wikipedia.org/wiki/Path_(computing). You are missing some fundamental knowledge to solve your problem. – CampbeII Mar 10 '19 at 19:51
0

You can use unlink() function to delete the image from directory.

If you image is the same directory of script and filename is in variable $id you can delete it by using unlink($id).

If your image is in another directory you can use

$fileid = 'path/'.$id
unlink($fileid)

Here path/ is the location of the directory in which image is stored.

Ajanyan Pradeep
  • 1,097
  • 1
  • 16
  • 26