Using Unlink to Delete File From Server

Discussion in 'PHP' started by toad78, Feb 4, 2009.

  1. #1
    I am having trouble deleting a file from the server when the delete record is set. Can someone take a look at this and tell me where I'm going wrong?

    if ((isset($_POST['empl_id'])) && ($_POST['empl_id'] != "")) {
      $deleteSQL = sprintf("DELETE FROM empl_dnlds WHERE empl_id=%s",
                           GetSQLValueString($_POST['empl_id'], "int"));
      
      $image_path = '../../../info/docs/employment/';
      
      if ((isset($_POST['empl_id'])) && file_exists($image_path.$_POST['empl_id'])) {
        unlink($image_path.$row_getApp['empl_dnld_fn']);
      }
      mysql_select_db($database_wvgsadmin, $wvgsadmin);
      $Result1 = mysql_query($deleteSQL, $wvgsadmin) or die(mysql_error());
    
      $deleteGoTo = "empl_app_list.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
        $deleteGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $deleteGoTo));
    }
    
    $colname_getApp = "-1";
    if (isset($_GET['empl_id'])) {
      $colname_getApp = $_GET['empl_id'];
    }
    PHP:

     
    toad78, Feb 4, 2009 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I suggest printing out the full path of the file you're trying to delete to make sure the file location is actually correct. Also, why are you checking if one file exists before deleting a separate file? You're using two separate variables for the end of the path. You're checking if "$image_path.$_POST['empl_id']" exists and then deleting "$image_path.$row_getApp['empl_dnld_fn']"
     
    zerxer, Feb 4, 2009 IP
  3. toad78

    toad78 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tested the location, and it's pointing to the right place.

    So your recommendation would be to do this to delete the file?:

    if ((isset($_POST['empl_id'])) && ($_POST['empl_id'] != "")) {
    $deleteSQL = sprintf("DELETE FROM empl_dnlds WHERE empl_id=%s",
    GetSQLValueString($_POST['empl_id'], "int"));

    $image_path = '../../../info/docs/employment/';

    unlink($image_path.$row_getApp['empl_dnld_fn']);
    ....
     
    toad78, Feb 4, 2009 IP
  4. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    zerxer is right, you are checking for one file, but then deleting the other. It's not clear from your snippet which one is correct, but they should be the same, either:
    
    if ((isset($_POST['empl_id'])) && file_exists($image_path.$_POST['empl_id'])) {
        unlink($image_path.$_POST['empl_id']);
    }
    
    PHP:
    or
    
    if ((isset($_POST['empl_id'])) && file_exists($image_path.$row_getApp['empl_dnld_fn'])) {
        unlink($image_path.$row_getApp['empl_dnld_fn']);
    }
    
    PHP:
     
    steelaz, Feb 4, 2009 IP