j.g. Posted July 16, 2007 Share Posted July 16, 2007 Hello All! I'm trying to implement the 'unlink' function to delete a file stored on the server; In this field it shows: Current Logo File: <fileName> Remove What I want, is for the user to be able to click on the 'Remove' link and have the file deleted from the server... Here is my code (that, unfortunately, is not working :-\ - or else, hence, I wouldn't be here): Current Logo File: <?php $file_query = "SELECT file_name FROM users, address WHERE users.user_id = '".$db->clean($current_user_id)."' " ." AND address.user_id = users.user_id" ." AND address.address_id = users.work_address_id"; $result_file = $db->query($file_query) or die("Error Getting Company Data"); while( $r = $db->fetch_row($result_file) ) { $filename = $r["file_name"]; } if ($filename != "") { echo $filename." <a href=unlink(/uploads/accounts/".$filename.");\">Remove</a><br>"; echo "</span>"; } ?> <br> '/uploads/accounts/' is the directory on the server where the file is stored. Any help and/or assistance that you could provide me would be greatly appreciated!! Thanks in advance for your time and expertise! -j.g. Quote Link to comment Share on other sites More sharing options...
lur Posted July 16, 2007 Share Posted July 16, 2007 You cannot link to a PHP function, you'll need to do something like ... echo $filename, ' <a href="page.php?delete=', $filename, '">Remove</a><br>'; .... if (FALSE == empty($_GET['delete'])) { // carefully delete the file ($filename = $_GET['delete']) } You'll need to be very careful and properly validate your input before deleting anything. Quote Link to comment Share on other sites More sharing options...
lococobra Posted July 16, 2007 Share Posted July 16, 2007 I think you're missing something very essential to this whole process... PHP is a server side language, unlike javascript, you can not call code on the fly. <?php //You're going to need to re-write this. echo $filename." <a href=unlink(/uploads/accounts/".$filename.");\">Remove</a><br>"; //Use something like... echo $filename." <a href=\"?del=".$filename.");\">Remove</a><br>"; //Then at the top of your script add... if(isset($_GET['del'])) unlink('/uploads/accounts/'.$_GET['del']); ?> It also might be good to add base64 encoding/decoding... or url encoding/decoding since you're sending this stuff through a link. EDIT: Yeah, lur's got it nailed. Quote Link to comment Share on other sites More sharing options...
j.g. Posted July 16, 2007 Author Share Posted July 16, 2007 Thanks for the replies!! They were very helpful and instrumental in my successful implementation of the function and executing what I wanted to do!! Below is my final solution - hopefully it will help someone else in the future! //deletes the file from the server after the user clicks the 'Remove' link if(isset($_GET['delete_file'])) { unlink($DOCUMENT_ROOT.'/uploads/accounts/'.$_GET['delete_file']); //sets file_name field in db to 'blank' after deleting from server $file_update_query = "UPDATE address, users SET " ."file_name = '".$db->clean($POST[" "][" "])."' " ."WHERE users.user_id = '".$db->clean($current_user_id)."' " ."AND address.user_id = users.user_id " ."AND address.address_id = users.work_address_id"; $result_file_update = $db->query($file_update_query) or die("Error Deleting Company Logo"); } $file_query = "SELECT file_name FROM users, address WHERE users.user_id = '".$db->clean($current_user_id)."' " ." AND address.user_id = users.user_id" ." AND address.address_id = users.work_address_id"; $result_file = $db->query($file_query) or die("Error Getting Company Data"); while( $r = $db->fetch_row($result_file) ){ $filename = $r["file_name"]; } //if filename exists, display the filename & a 'Remove' link if ($filename != "") { echo $filename." <a href='company_edit.php?delete_file=".$filename."');\">Remove</a><br>"; echo "</span>"; } Thanks again!! -j.g. Quote Link to comment 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.