richard_PHP Posted June 26, 2012 Share Posted June 26, 2012 Hello, I'm wanting to create a script which deletes an image from both a database and off the directory. I understand that the code needed to remove from the directory is UNLINK, I'm having trouble finding how to put it into my code. As follows: <?php $conn = mysql_pconnect("#####", "###", "####"); mysql_select_db("###", $conn); $choice = $_POST[choice]; mysql_query("DELETE FROM ## WHERE id='$choice'"); echo "<p>Image has been deleted.</p>"; echo "<p>Click <a href='adminphotos.php'>here</a> to delete another.</p>"; ?> So our user has selected with a radio button which image they would like to delete and this page processes that. At the moment the above code removes from the database. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/264838-unlink-from-a-choice/ Share on other sites More sharing options...
-Karl- Posted June 26, 2012 Share Posted June 26, 2012 You just need the full URL to the file then you can do: $url = '/public_html/images/image1.png'; unlink($url); http://www.php.net/manual/en/function.unlink.php Also, just so you know, you must have the correct CHMOD permissions in order to do this. Quote Link to comment https://forums.phpfreaks.com/topic/264838-unlink-from-a-choice/#findComment-1357240 Share on other sites More sharing options...
richard_PHP Posted June 26, 2012 Author Share Posted June 26, 2012 In the previous page I have pulled the information from the database (id and imgurl) for use in this process. The above code uses the id number as choice and removes where they match. Could I just have the unlink as follows: $imgurl = $_POST[imgurl]; unlink($url); Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/264838-unlink-from-a-choice/#findComment-1357243 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 In the previous page I have pulled the information from the database (id and imgurl) for use in this process. The above code uses the id number as choice and removes where they match. Could I just have the unlink as follows: $imgurl = $_POST[imgurl]; unlink($url); Cheers. yes mate as long as the $_POST['imgurl is the full path to the file if not then you must put unlink("/path/to/files/".$imgurl); Quote Link to comment https://forums.phpfreaks.com/topic/264838-unlink-from-a-choice/#findComment-1357251 Share on other sites More sharing options...
richard_PHP Posted June 26, 2012 Author Share Posted June 26, 2012 Brilliant! Many thanks for the help both of you Quote Link to comment https://forums.phpfreaks.com/topic/264838-unlink-from-a-choice/#findComment-1357253 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 your welcome.. Quote Link to comment https://forums.phpfreaks.com/topic/264838-unlink-from-a-choice/#findComment-1357255 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.