Adrian
Adrian

Reputation: 61

If file exist returns else note

I have the following code:

if(file_exists("../media/books/$oldcover")) {
    unlink("../media/books/$oldcover");
} else { 
    //do nothing 
} 

Why does the if works good and the else returns this annoying note: Unlink(../media/books/) [function.unlink]: Is a directory in (path...)

I want the else doing nothing.

Upvotes: 0

Views: 40

Answers (2)

Ben Rowe
Ben Rowe

Reputation: 28721

Sounds like your $oldcover variable could be empty and thus is checking if the directory exists or not.

This then tries to unlink the directory instead. Perhaps you should try this:

if($oldcover && file_exists("../media/books/$oldcover")) {
  unlink("../media/books/$oldcover");
} else { 
  //do nothing 
} 

Upvotes: 1

Tristan
Tristan

Reputation: 3321

It appears your variable $oldcover is empty. You could also change the conditional to check if the file exists and that it is not a directory.

if(file_exists("../media/books/$oldcover") && !is_dir("../media/books/$oldcover"))

Upvotes: 1

Related Questions