SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Unlink, but not a certain file

    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

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    for clarity I would use an if else
    PHP Code:
        if(isset($_POST['empl_dnld_fn']) && $_POST['empl_dnld_fn'] == 'nophotoavailable.jpg') {
            echo 
    'That file cannot be deleted';
        } else {
            if(isset(
    $_POST['empl_dnld_fn']) && file_exists($image_path.$_POST['empl_dnld_fn'])) {
                
    unlink($image_path.$_POST['empl_dnld_fn']);
            } 
        } 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by spikeZ View Post
    for clarity I would use an if else
    [php]
    if(isset($_POST['empl_dnld_fn']) && $_POST['empl_dnld_fn'] == 'nophotoavailable.jpg') {
    echo 'That file cannot be deleted';
    }
    Thanks for getting back to me! I really don't need to echo, just the record to delete without the 'nophotoavailable.jpg' getting deleted.

    Would this still work?

    toad78

  4. #4
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (isset($_POST['empl_dnld_fn']) && $_POST['empl_dnld_fn'] != 'nophotoavailable.jpg') {
        @
    unlink($image_path.$_POST['empl_dnld_fn']);

    In a test I just ran, suppressing the error instead of calling file_exists is about 6x faster.
    PHP Code:
    <?php
    $start_time 
    microtime(true);
    for (
    $x=0$x<300$x++) {
        @
    unlink('this_file_does_not_exist.php');
    }
    $end_time microtime(true);
    $total_time $end_time $start_time;
    echo 
    '<br>Time spent with @unlink: '.$total_time.' seconds';

    $start_time microtime(true);
    for (
    $x=0$x<300$x++) {
        if (
    file_exists('this_file_does_not_exist.php')){
            
    unlink('this_file_does_not_exist.php');
        }
    }
    $end_time microtime(true);
    $total_time $end_time $start_time;
    echo 
    '<br>Time spent with file_exists: '.$total_time.' seconds';
    ?>

  5. #5
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    huh?

  6. #6
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try the 3 line script above in place of what he had.
    The rest of my post was just explaining why I chose my method over his (although his is technically more correct in my opinion).

    The error I was referring to is a warning PHP throws if you try to delete a file that doesn't exist.
    In my script, I'm essentially "hiding" the error. In his script, he's checking to see if the file exists before unlinking.

  7. #7
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    OH! Okay! Thank you!

    toad78

  8. #8
    SitePoint Guru
    Join Date
    Jul 2003
    Location
    england
    Posts
    819
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simshaun View Post
    Try the 3 line script above in place of what he had.
    Thank you! It worked out very well!


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
  •