Odd unlink behaviour

Hi there,

So I’ve created a custom function delDirContents:


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:


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

Should it unlink(“/path/to/dir/” . $file) maybe?

Also, your function returns nothing at all - so how does the test for false work here?


if(!delDirContents('path/to/dir/')){
// 
}