SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Odd unlink behaviour

    Hi there,

    So I've created a custom function delDirContents:
    PHP Code:
    function delDirContents($dir);
      if(
    $handle opendir($dir)){
        while(
    false !== ($file readdir($handle))){
          if(
    $file != '.' && $file != '..'){
            
    unlink($file);
          }
        }
        
    closedir($handle);
      }

    Which I use twice in a script. It works fine on one occasion, but not on the other. Here's where it throws an error:
    PHP Code:
    if(isset($_POST['cancel'])){
      if(!
    delDirContents('path/to/dir/')){
        echo 
    'Unable to empty directory';
        exit();
      }
      
    header('Location: /');

    The error I get is:
    Warning: unlink(Filename.png): No such file or directory in /path/to/script.php on line n

    Which is very odd, because the function got the filename, and it does exists, but I have no idea why it won't delete the file. Can anyone shed some light onto this?

    Cheers,
    Mike

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Should it unlink("/path/to/dir/" . $file) maybe?

    Also, your function returns nothing at all - so how does the test for false work here?
    PHP Code:
    if(!delDirContents('path/to/dir/')){
    // 



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
  •