Jump to content

[SOLVED] How do I create a link to delete an image?


sketchi

Recommended Posts

Hello,

 

I'm making a really simple cms, but having problem deleting images. I've managed to create a page to upload the images to a folder. I also have a page that shows all the images and file names. Now I'm trying to add a link to delete images, but I can't get it to work. First of all I don't know what to put in the href tag...? Someone said I should create a new page that deletes the image and then redirects you back to the page displaying the images...?

 

I probably need quite detailed instructions, as I'm totally new to php.... Here's what I have so far:

 

<?

 

$thumbs_dir = "../images/";

$dir = opendir($thumbs_dir);

 

while ($file = readdir($dir)) {

      if ($file != "." && $file != ".." ) {

          echo "<tr><td style='border-bottom:#000 1px solid;'><img src='$thumbs_dir$file' border='0' /></td><td style='border-bottom:#000 1px solid;'>$file <a href=\"$file_delete\">Delete</a></td></tr>";

    }

}

 

$file_delete = "$thumbs_dir$file";

 

  if (unlink($file_delete)) {

      echo "The file was deleted successfully.", "\n";

  } else {

      echo "The specified file could not be deleted. Please try again.", "\n";

  }

 

?>

you can pass variables in the url

 

phppage.php?delete=yes&anothervar=somethingelse

 

then you can have an if statement

if (isset($_GET['delete']) && $_GET['delete'] == "Yes") {

//delete the picture

}

 

you can pass all kinds of things back to the same page, and with a series of if/else statements, do all kinds of stuff :)

You would use unlink($filename) in the area where you are checking to see if you are trying to delete it like this.

 

if (isset($_GET['delete']) && $_GET['delete'] == "Yes") {

unlink(mypic.jpg);

}

 

That should work.

 

IF unlink() by itself does not work on your setup, use fopen() first, then unlink().

 

There is no native delete function in php, unlink does that.

I get an error when I try this (I guess I haven't done it right):

 

Parse error: syntax error, unexpected T_VARIABLE in /file/path/admin/deleteimage.php on line 26

 

 

Here's what I've done:

 

<?

 

$thumbs_dir = "../images/";

$dir = opendir($thumbs_dir);

 

while ($file = readdir($dir)) {

      if ($file != "." && $file != ".." ) {

          echo "<tr><td style='border-bottom:#000 1px solid;'><img src='$thumbs_dir$file' border='0' /></td><td style='border-bottom:#000 1px solid;'>$file <a href=\"deleteimage.php?delete=yes\">Radera</a></td></tr>";

    }

}

 

if (isset($_GET['delete']) && $_GET['delete'] == "Yes") {

unlink($thumbs_dir$file);

}

 

?>

I've changed it to unlink($thumbs_dir.$file); but now it just seems to reload the page and doesn't do anything...

 

I guess somehow I need to specify in the delete link which image to delete? I took a guess on this but that didn't work either:

 

<a href=\"deleteimage.php?$thumbs_dir$file&delete=yes\">Delete</a>

Yay, solved it!  :D

 

Here's the complete code to display all images in a folder and be able delete them one by one through clicking a link:

 

images.php

 

$thumbs_dir = "../images/";
$dir = opendir($thumbs_dir);

while ($file = readdir($dir)) {
      if ($file != "." && $file != ".." ) {
           echo "<tr><td class='divider'><img src='$thumbs_dir$file' border='0' /></td><td class='divider'>$file</td><td class='divider'><a href=\"deleteimage.php?imageid=$file&action=delete\">» Delete</a></td></tr>";
     }
}

 

deleteimage.php

 

$thumbs_dir = "../images/";
$dir = opendir($thumbs_dir);

if (isset($action) && ($action == "delete"))
    {
$imageid = $_GET['imageid'];
    unlink($thumbs_dir.$imageid);
   }
?>

 

There apparently needs to be a link back to the images.php page from deleteimage.php, using the browser back button won't do as it won't refresh the page properly.

 

Cool  ;D

if you want to delete the image and remain on the same page

 

when you say :

if (isset($action) && ($action == "delete"))

 

then you can use echo file_get_contents('urltoyourdeletephppage');

 

and simply echo the results of the delte, maybe you display a success statement or not

this way you can use file_get_contents() function to load another page, yet say on the same page

 

take advantage of this :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.