optikalefx Posted August 4, 2007 Share Posted August 4, 2007 i have a few pictures that are generated from a database. It goes into my database and returns urls and then echo "<a href='http://www.4tenproductions.com/ir/uploadedfiles2/" . $row['pic'] . "'><img style='border-width:4;border-style:double;border-color:black' src='http://www.4tenproductions.com/ir/uploadedfiles/resize_" . $row['pic'] . "'</a>"; so i have a bunch of clickable pictures. I want a way to click a picture and it gets deleted. i know how to delete using unlink, so basically i need to figure out how to return the name of the file clicked on... Idk if there is a function in PHP for that, or if javascript is required or what, but any help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/ Share on other sites More sharing options...
cooldude832 Posted August 4, 2007 Share Posted August 4, 2007 javascript ajax make the links into an ajax query that will delete the image on click (without client knowing) and display it all in 1 blow. Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315348 Share on other sites More sharing options...
optikalefx Posted August 4, 2007 Author Share Posted August 4, 2007 will it be able to delete from the database too? it needs to delete 3 different places. //Delete from server thumbnail $file1 = "http://www.4tenproductions.com/ir/uploadedfiles/resize_" . $filename; unlink($file1); //Delete from server picture $file2 = "http://www.4tenproductions.com/ir/uploadedfiles2/" . $filename; unlink($file2); //Delete from database $sql2 = mysql_query("DELETE FROM pics WHERE pic ='$file2'") Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315350 Share on other sites More sharing options...
optikalefx Posted August 4, 2007 Author Share Posted August 4, 2007 its $filename that i cant get Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315352 Share on other sites More sharing options...
pyrodude Posted August 4, 2007 Share Posted August 4, 2007 or you could set your href to www.yoursite.com/image/image.php?img= $row['pic'] and then in image.php check for $_GET['img'] Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315353 Share on other sites More sharing options...
optikalefx Posted August 4, 2007 Author Share Posted August 4, 2007 yea that got me the right name good. but is unlink the way to delete a file from the server? because its not working... <?PHP $filename = $_REQUEST["image"]; //Delete from server $file1 = "http://www.4tenproductions.com/ir/uploadedfiles/resize_" . $filename; unlink($file1); $file2 = "http://www.4tenproductions.com/ir/uploadedfiles2/" . $filename; unlink($file2); //Delete from database $sql2 = mysql_query("DELETE FROM pics WHERE pic ='$file2'"); echo "Image Deleted"; ?> it says image delete, but its not deleting anything...from any of the 3 places Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315360 Share on other sites More sharing options...
pyrodude Posted August 4, 2007 Share Posted August 4, 2007 where is $row['pic'] generated? I mean, do you want to delete $file2 from the database, or $row['pic']? also, you'll need to delete the local file (unlink($filename)) rather then the URL to remove the actual image file Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315364 Share on other sites More sharing options...
optikalefx Posted August 4, 2007 Author Share Posted August 4, 2007 row['pic'] is coming from a select query. $sql = mysql_query("SELECT pic FROM pics WHERE user='$table'"); you said that i need to unlink($filename) but how does it know where that file is located on my server? i read from a site that its supposed to be the url that is getting unlinked i think im using it wrong. i also have an odd query which im not sure will work, but its this $sql2 = mysql_query("DELETE FROM pics WHERE pic='$filename' and user='$table'"); i dont know if i can use and in there. the other option is to add another row in there to keep a unique number. but id rather use and if i can. i have like 8 pictures all with the user name JohnRobberts. so i cant say delete where user=johnrobberts, and i cant say just delete where pic ="url" because some other user might have the same url, so i need to specify both, and id rather not add that 3rd row if i have to. i still think im using unlink wrong, but it cant work with just $filename, because it wont know where to find it.. Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315368 Share on other sites More sharing options...
optikalefx Posted August 4, 2007 Author Share Posted August 4, 2007 ok so making progress i added the 3rd column, it only took 3 seconds... lol, and i 4got to connect to the database. so now thats working, but it still wont delete from 2 spots on the server. <?PHP $con = mysql_connect("xxxx","xxxx","xxxx"); mysql_select_db("update1", $con); $filename = $_REQUEST["image"]; $table = $_REQUEST["table"]; $num = $_REQUEST["num"]; echo $filename; echo $table; echo $num; //Delete from server DOESNT WORK $file1 = "/ir/uploadedfiles/resize_" . $filename; unlink($file1); $file2 = "/ir/uploadedfiles2/" . $filename; unlink($file2); //Delete from database WORKS $sql2 = mysql_query("DELETE FROM pics WHERE num='$num'"); echo "Image Deleted"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315378 Share on other sites More sharing options...
pyrodude Posted August 4, 2007 Share Posted August 4, 2007 Well, whoever told you unlink() needed to be the url was somewhat right. It needs to be the path to the file from the current working directory. So, if it's up 1 directory and then in the images folder, it would be $file = '../images/' . $filename; unlink($file); Am I making any sense? On an unrelated note, you should be careful using $_REQUEST, as it's rather susceptible to attacks (especially if you're not doing any security checks on the input) If it still doesn't work, perhaps add an echo $file1 prior to the unlink, or even change it to unlink($file1) or die("Unable to delete: $file1"); Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315429 Share on other sites More sharing options...
optikalefx Posted August 5, 2007 Author Share Posted August 5, 2007 well the php file is located at 4tenproductions.com the files i want to delete are located at 4tenproductions.com/ir/uploadedfiles2 and 4tenproductions.com/ir/uploadedfiles so would it be $file1 = "/ir/uploadedfiles/resize_" . $filename; because thats what im using, and its not working. just to clarify: php file is at: http://www.4tenproductions.com/jrdelpic2.php file1 is at: http://www.4tenproductions.com/ir/uploadedfiles/ file2 is at: http://www.4tenproductions.com/ir/uploadedfiles2/ and instead of request, what is safer GET or POST? because a lot of my form methods are POST so thats fine, but a lot of variables are passed thru the URL too so id have to use GET but i heard GET is less secure than POST... Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315867 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 unlink("./ir/uploadedfiles/$filename"); unlink("./ir/uploadedfiles2/$filename"); This is a relative path from where the script is being executed. Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-315871 Share on other sites More sharing options...
pyrodude Posted August 5, 2007 Share Posted August 5, 2007 You should use whatever form is being used to pass the variable. If you're passing it via link, the only option is GET. It's less secure as it's susceptible to tampering from people, but it's also the only option. However, REQUEST also calls from cookies, so you just need to be careful. Quote Link to comment https://forums.phpfreaks.com/topic/63269-unlink-find-the-url-of-clicked-image/#findComment-316191 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.