Jump to content

[SOLVED] Newbie ? - Help with unlink function


j.g.

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/60243-solved-newbie-help-with-unlink-function/
Share on other sites

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.