oraya Posted June 26, 2012 Share Posted June 26, 2012 Below is the delete file for our Brochure deletes via a delete button on the update page. The upload form uploads the file to the "downloads" folder, and also puts the following into the brochures table. id, title, description and also the full file name example flowers.pdf (which is used for the links). What I want to be able to do on the delete script below, is to not only delete the item if id matches from the table, but also delete the pdf file from the downloads folder before going to the success page via the header. I tried to add unlink to the script but it didn't work, I probably wasn't doing it correctly. Hence why I am here. Is there anyway someone could look at it and tell me how to add this feature to my delete script? Ok hope I've explained this ok, many thanks in advance for any help given. Oraya <?php $host = "localhost"; $user = "myusername"; $pass = "mypassword"; $database = "mydatabasename"; $conn = mysql_connect($host, $user, $pass) or die( mysql_error() ); mysql_select_db($database) or die( mysql_error() ); $table = mysql_real_escape_string( $_GET["table"] ); $id = mysql_real_escape_string( $_GET["id"] ); $message = mysql_real_escape_string( $_GET["message"] ); $redirect = mysql_real_escape_string( $_GET['redirect'] ); $file = mysql_real_escape_string( $_GET['file'] ); $sql="DELETE FROM $table WHERE id='$id'"; $result = mysql_query( $sql ) or die( mysql_error() ); if($result) { header("Location: ../successful.php?message=$message&redirect=$redirect"); }else{ echo"there was a problem!"; } mysql_close($conn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/ Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 here <?php $host = "localhost"; $user = "myusername"; $pass = "mypassword"; $database = "mydatabasename"; $conn = mysql_connect($host, $user, $pass) or die( mysql_error() ); mysql_select_db($database) or die( mysql_error() ); $table = mysql_real_escape_string( $_GET["table"] ); $id = mysql_real_escape_string( $_GET["id"] ); $message = mysql_real_escape_string( $_GET["message"] ); $redirect = mysql_real_escape_string( $_GET['redirect'] ); $file = mysql_real_escape_string( $_GET['file'] ); unset("path/to/download/folder/".$file); $sql="DELETE FROM $table WHERE id='$id'"; $result = mysql_query( $sql ) or die( mysql_error() ); if($result) { header("Location: ../successful.php?message=$message&redirect=$redirect"); }else{ echo"there was a problem!"; } mysql_close($conn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357205 Share on other sites More sharing options...
oraya Posted June 26, 2012 Author Share Posted June 26, 2012 Thank you dc2007 for your really quick reply, it throws the following error on the unset line "syntax error unexpected T_CONSTANT_ENCAPSED_STRING" Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357208 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 what does $file return ? and whats the path do the downloads folder ? Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357209 Share on other sites More sharing options...
oraya Posted June 26, 2012 Author Share Posted June 26, 2012 file is the file name such as flowers.pdf and the download path is ../downloads Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357210 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 change unset to unlink mate Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357211 Share on other sites More sharing options...
oraya Posted June 26, 2012 Author Share Posted June 26, 2012 Nope still not deleting it sigh... could it be because I'm trying to do it on my laptop testing area? And it's a case that it will work once uploaded to the host? Though I would have thought it would still work as I have the latest php, mysql etc etc installed. I'm kinda new to it all so forgive all the questions. Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357214 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 is the downloads folder in the same folder as the php file is so replace the code with: <?php $host = "localhost"; $user = "myusername"; $pass = "mypassword"; $database = "mydatabasename"; $conn = mysql_connect($host, $user, $pass) or die( mysql_error() ); mysql_select_db($database) or die( mysql_error() ); $table = mysql_real_escape_string( $_GET["table"] ); $id = mysql_real_escape_string( $_GET["id"] ); $message = mysql_real_escape_string( $_GET["message"] ); $redirect = mysql_real_escape_string( $_GET['redirect'] ); $file = mysql_real_escape_string( $_GET['file'] ); unlink("./downloads/".$file); $sql="DELETE FROM $table WHERE id='$id'"; $result = mysql_query( $sql ) or die( mysql_error() ); if($result) { header("Location: ../successful.php?message=$message&redirect=$redirect"); }else{ echo"there was a problem!"; } mysql_close($conn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357216 Share on other sites More sharing options...
oraya Posted June 26, 2012 Author Share Posted June 26, 2012 no the delete script is in a folder called includes so that I can keep the database login details secure hence the ../ do I need it in the same folder? If so I can use an include for the log in details. Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357219 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 change unlink("./downloads/".$file); to unlink("../downloads/".$file); Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357220 Share on other sites More sharing options...
oraya Posted June 26, 2012 Author Share Posted June 26, 2012 I've already done that, it didn't help. Any other ideas? I'm so grateful for your help by the way. I've been googling this for five hours trying to sort it myself before coming here. Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357222 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 try replacing downloads with you whole path to the file unlink("/path/to/file/downloads/".$file); Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357223 Share on other sites More sharing options...
oraya Posted June 26, 2012 Author Share Posted June 26, 2012 Ok just tried that, that didn't work either sigh... right I'm going to try uploading the script and see if it works on my web host will be back in a little while. Thank you so much for this help. Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357226 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 looking at the script its not querying the database to find the file name add under unlink(); add echo $file; what does it return Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357227 Share on other sites More sharing options...
oraya Posted June 26, 2012 Author Share Posted June 26, 2012 Will you marry me? lol... I tried uploading it and testing it on the host, still didn't work then came back and was about to send you a message when I noticed your comment about echoing the file. Bingo... I hadn't sent the filename in the links sigh... Thank you thank you thank you.... I would still be here this time tomorrow if it wasn't for you! And thank you sooooooooooo much for your patience!!! Huge kiss for ya.. I'm so very very grateful... Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357245 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 Will you marry me? lol... I tried uploading it and testing it on the host, still didn't work then came back and was about to send you a message when I noticed your comment about echoing the file. Bingo... I hadn't sent the filename in the links sigh... Thank you thank you thank you.... I would still be here this time tomorrow if it wasn't for you! And thank you sooooooooooo much for your patience!!! Huge kiss for ya.. I'm so very very grateful... if you are a female i will lol glad i could help. Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357246 Share on other sites More sharing options...
oraya Posted June 26, 2012 Author Share Posted June 26, 2012 I am lol... Oh seriously I've been trying to sort this since very early this evening using google.. it was starting to drive me insane You really have no idea just how happy you've made me. THANK YOU!!! One last question where is the SOLVED button? lol.. Can't seem to see it. Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357247 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 Good lol nice to meet a girl whos interested in php coding lol whats your site will take a look and if you need anymore help im around. p.s i think you need to edit your first post to change status to solved.. David. Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357249 Share on other sites More sharing options...
oraya Posted June 26, 2012 Author Share Posted June 26, 2012 It's for a friend lol.. A funeral directors haha... I know radical!! I haven't got it up on a web hosting site as such, well until they register for one later in the week. So just for the testing of that script I signed up for a free host to test it lol.. And I've only uploaded the management area at the moment. I'll inbox you the proper link at a later date if that's ok! Once again thank you soooo very much. Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357258 Share on other sites More sharing options...
dc2007 Posted June 26, 2012 Share Posted June 26, 2012 you very welcome that would be fine love. Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357261 Share on other sites More sharing options...
oraya Posted June 26, 2012 Author Share Posted June 26, 2012 You are a star!!! I can now head off to bed, knowing I don't have to tackle that tomorrow lol.. Have a great evening! Quote Link to comment https://forums.phpfreaks.com/topic/264832-adding-a-file-delete-to-script/#findComment-1357263 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.