Results 1 to 9 of 9

Thread: Unlink to Delete File From Server

  1. #1
    Join Date
    Mar 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Unlink to Delete File From Server

    I'm having trouble deleting a filename from a server after a record deletion is set. Can someone take a look at this and tell me what I'm doing wrong? The record on the db deletes, but not the file on the server.

    PHP Code:
    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'];


  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Are you sure your doing it right?

    For example: /examples/page1/page2/page3/page4/page5

    Now, I want to get back to page3, I would do:
    Code:
    ../ (page4) ../ (page3)
    So for example, in this case your going to page 2, then going to info/docs/empoyment/$file.
    Jeremy | jfein.net

  3. #3
    Join Date
    Mar 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Quote Originally Posted by Nile View Post
    Are you sure your doing it right?
    I've found out the pickle:
    I forgot to add a hidden field in my form for the filename. I've ran it through a few tests and it works great now. WOOHOO!

    But now I have the problem of trying to delete duplicate files. For example, say someone inserts a new record with the same filename. I have the insert form coded to where if the filename is a duplicate, create a date/timstamp on it. Well, let's say they want to delete the file with the record, but the record has a date/timestamp on it. For some reason, my delete form doesn't delete the date/timestamp file that is related to it. It deletes the record fine, just not the file.

    I thought maybe I should change the "unlink($image_path.$_POST['empl_dnld_fn']);" to "unlink($image_path.time($_POST['empl_dnld_fn']));" IF THE FILE HAS A DATE/TIMESTAMP. But I don't know if this would work. Do you have any suggestions?

    PHP Code:
    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_dnld_fn']) && file_exists($image_path.$_POST['empl_dnld_fn'])) {
          
    unlink($image_path.$_POST['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));


  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I don't completely get what you mean...
    Can you be more descriptive? If you need to determine what time the file has been created, use:
    PHP Code:
    <?php
    $filename 
    $_POST['file'];
    echo 
    date ("m/d/y h:m"filemtime($filename));
    ?>
    Jeremy | jfein.net

  5. #5
    Join Date
    Mar 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    What I have is a file upload form that if the file is a duplicate being uploaded, it creates a date/timestamp added to the filename using 'PHPUPLOAD_DIR.time().$file);'

    In my delete record form, I have the selected record to delete with a filename associated with it. If that filename was a duplicate that has the date/timestamp on it, for some reason my delete form is not deleting the duplicate file from the server that is associated with that specific record.

    I hope this helps!

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    What do you mean by 'duplicate', do you mean:
    Code:
    Folder1 --
    --Index.php
    --Index.php
    Jeremy | jfein.net

  7. #7
    Join Date
    Mar 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    More like:
    contract.pdf
    2009-02-05-225853contract.pdf (duplicate)

  8. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Ahh... I see:
    (This is very dangerous):
    PHP Code:
    <?php
      $unlink_vars 
    glob("*".$_POST['file']);
      foreach(
    $unlink_vars as $file){
        
    unlink($file);
      }
    ?>
    WARNING: Using the glob() and unlink() function together can sometimes result in bad things such as deleting all the files.
    MAKE SURE THAT $_POST['file'] ISN'T BLANK!

    Now - there are some bugs. For example:
    Code:
    Folder1--
    --index.php
    --pic.png
    --picture.png
    If someone types 'png', it'd delete pic.png and picture.png.
    Jeremy | jfein.net

  9. #9
    Join Date
    Mar 2008
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    I decided to take a different route and got it working in a different way.

    But I have another question:
    I am using the unlink function to delete a related filename in a record. BUT, what if I have a specific file that I don't want deleted?

    For example, in my insert form, I have a list of images from a folder. Whatever is picked for that record is linked. But what if it is a file called 'nophotoavailable.jpg'? If I have a record that doesn't have a photo and they select this particular file and eventually want to delete the record, but I don't want this file deleted, how do I construct unlink to work in this case?

    I currently have this:
    PHP Code:
      $image_path '../../../info/docs/employment/';
        if (isset(
    $_POST['empl_dnld_fn']) && file_exists($image_path.$_POST['empl_dnld_fn'])) {
          
    unlink($image_path.$_POST['empl_dnld_fn']);
          } 
    and am not sure how I don't want file 'nophotoavailable.jpg' (which is coming from 'empl_dnld_fn') to NOT be deleted if the record gets deleted.

    I appreciate any insight!

    Thank you!
    toad78

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •