sketchi Posted April 25, 2007 Share Posted April 25, 2007 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"; } ?> Link to comment https://forums.phpfreaks.com/topic/48636-solved-how-do-i-create-a-link-to-delete-an-image/ Share on other sites More sharing options...
dsaba Posted April 25, 2007 Share Posted April 25, 2007 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 Link to comment https://forums.phpfreaks.com/topic/48636-solved-how-do-i-create-a-link-to-delete-an-image/#findComment-238159 Share on other sites More sharing options...
sketchi Posted April 25, 2007 Author Share Posted April 25, 2007 ok.... so what exactly would I have to do to get it to work...? :-\ Link to comment https://forums.phpfreaks.com/topic/48636-solved-how-do-i-create-a-link-to-delete-an-image/#findComment-238161 Share on other sites More sharing options...
rcorlew Posted April 25, 2007 Share Posted April 25, 2007 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. Link to comment https://forums.phpfreaks.com/topic/48636-solved-how-do-i-create-a-link-to-delete-an-image/#findComment-238169 Share on other sites More sharing options...
sketchi Posted April 25, 2007 Author Share Posted April 25, 2007 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); } ?> Link to comment https://forums.phpfreaks.com/topic/48636-solved-how-do-i-create-a-link-to-delete-an-image/#findComment-238180 Share on other sites More sharing options...
dsaba Posted April 25, 2007 Share Posted April 25, 2007 change unlink($thumbs_dir$file); to: unlink($thumbs_dir.$file); if you're using 2 variables you have to concactate them bool unlink ( string $filename [, resource $context] ) Link to comment https://forums.phpfreaks.com/topic/48636-solved-how-do-i-create-a-link-to-delete-an-image/#findComment-238221 Share on other sites More sharing options...
sketchi Posted April 25, 2007 Author Share Posted April 25, 2007 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> Link to comment https://forums.phpfreaks.com/topic/48636-solved-how-do-i-create-a-link-to-delete-an-image/#findComment-238261 Share on other sites More sharing options...
sketchi Posted April 25, 2007 Author Share Posted April 25, 2007 Yay, solved it! 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 Link to comment https://forums.phpfreaks.com/topic/48636-solved-how-do-i-create-a-link-to-delete-an-image/#findComment-238427 Share on other sites More sharing options...
dsaba Posted April 26, 2007 Share Posted April 26, 2007 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 Link to comment https://forums.phpfreaks.com/topic/48636-solved-how-do-i-create-a-link-to-delete-an-image/#findComment-238841 Share on other sites More sharing options...
sketchi Posted April 26, 2007 Author Share Posted April 26, 2007 Sorry, I don't quite understand. What bits of code should go in which page? What exactly should I change in the code I currently have (see my previous post)? It would be great not to have to go to a new page! Link to comment https://forums.phpfreaks.com/topic/48636-solved-how-do-i-create-a-link-to-delete-an-image/#findComment-238866 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.