40

I have a folder where I keep my images, named img/. I have a table with all of my images:

<table border="3">
    <tr>
        <td>    
            <?php
            $files = glob("img/*");
            foreach ($files as $file) {
                echo "<div class='divimages'>"; 
                echo '<img src="'.$file.'"/>';
                echo "<input type='submit' value='Delete image'/><br>";
                echo "</div>";  
            }
            ?>
        </td>
    </tr>   
</table>

How can I delete the image associated to the button with the value:"Delete image".

Brandon
  • 16,382
  • 12
  • 55
  • 88
emcee22
  • 1,851
  • 6
  • 23
  • 32
  • using mcryans answer below you will have to get the button to post a form with the filename(s) in the post data then loop through the list of file names and use the unlink function in php. – azzy81 Feb 21 '13 at 15:50

8 Answers8

73

There are a few routes. One, the most simple, would involve making that into a form; when it submits you react to POST data and delete the image using unlink

DISCLAIMER: This is not secure. An attacker could use this code to delete any file on your server. You must expand on this demonstration code to add some measure of security, otherwise you can expect bad things.

Each image's display markup would contain a form something like this:

echo '<form method="post">';
  echo '<input type="hidden" value="'.$file.'" name="delete_file" />';
  echo '<input type="submit" value="Delete image" />';
echo '</form>';

...and at at the top of that same PHP file:

if (array_key_exists('delete_file', $_POST)) {
  $filename = $_POST['delete_file'];
  if (file_exists($filename)) {
    unlink($filename);
    echo 'File '.$filename.' has been deleted';
  } else {
    echo 'Could not delete '.$filename.', file does not exist';
  }
}
// existing code continues below...

You can elaborate on this by using javascript: instead of submitting the form, you could send an AJAX request. The server-side code would look rather similar to this.

Documentation and Related Reading

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
  • 2
    No problem! Take care to add some security provisions there, otherwise it could end in tears :P. Some ideas for security, depending on how you're using it: .htaccess password requirement; checking that the `$filename` is in the image folder before deleting it; session-based user login; or PHP-based password protection. – Chris Baker Feb 21 '13 at 15:46
33

You can delete files in PHP using the unlink() function.

unlink('path/to/file.jpg');
mcryan
  • 1,566
  • 10
  • 20
5

First Check that is image exists? if yes then simply Call unlink(your file path) function to remove you file otherwise show message to the user.

              if (file_exists($filePath)) 
               {
                 unlink($filePath);
                  echo "File Successfully Delete."; 
              }
              else
              {
               echo "File does not exists"; 
              }
Wajid khan
  • 842
  • 9
  • 18
3

For deleting use http://www.php.net/manual/en/function.unlink.php Hope you'll can to write logic?

Winston
  • 1,758
  • 2
  • 17
  • 29
2

You can try this code. This is Simple PHP Image Deleting code from the server.

<form method="post">
<input type="text" name="photoname"> // You can type your image name here...
<input type="submit" name="submit" value="Delete">
</form>

<?php
if (isset($_POST['submit'])) 
{
$photoname = $_POST['photoname'];
if (!unlink($photoname))
  {
  echo ("Error deleting $photoname");
  }
else
  {
  echo ("Deleted $photoname");
  }
}
?>
Obaidul Haque
  • 916
  • 11
  • 18
1
<?php

    require 'database.php';

    $id = $_GET['id'];

    $image = "SELECT * FROM slider WHERE id = '$id'";
    $query = mysqli_query($connect, $image);
    $after = mysqli_fetch_assoc($query);

    if ($after['image'] != 'default.png') {
        unlink('../slider/'.$after['image']);
    }

    $delete = "DELETE FROM slider WHERE id = $id";
    $query = mysqli_query($connect, $delete);

    if ($query) {
        header('location: slider.php');
    }

?>
Derk Jan Speelman
  • 11,291
  • 4
  • 29
  • 45
0
<?php
$path = 'img/imageName.jpg';
if (is_file($path)) {
    unlink($path);
} else {
    die('your image not found');
}
Thelonias
  • 2,918
  • 3
  • 29
  • 63
0

For anyone wanting to delete all .png files starting from current directory, I've created a small script. It iterates over all available folders (one level only), searches for all .png file and deletes them:

$dirs = array_filter(glob('*'), 'is_dir');

foreach ($dirs as $directory) {
    $images = glob($directory . "/*.png");

    foreach ($images as $image) {
        echo "DELETING: " . $image . "<br>";
        unlink($image);
    }
}

if the script is located in app folder (app/index.php) it will search (one level) in all app/{folderName} for .png files and delete them. You can change the .png to .jpg or what you need.

Lepy
  • 152
  • 1
  • 12