Page 1 of 1

unlink()

Posted: Fri May 04, 2007 12:39 pm
by guitarlvr
I have a script which compares two files against each other. Once the comparison has been made, i want to delete the files from the server.

This does not work. It deletes the first and not the second:

Code: Select all

if ((!unlink($file1)) && (!unlink($file2))){
     echo 'files could not be deleted';
}
This, however, does work:

Code: Select all

unlink($file1);
unlink($file2);
if ((file_exists($file1)) || (file_exists($file2))){
	echo 'Files could not be deleted from server.';
}
Can anyone see why the if statement didn't work?

Wayne

Posted: Fri May 04, 2007 12:56 pm
by blackbeard
Because as soon as the first file is deleted, the left side of the expression is going to be false. Unlink will return true when the file is deleted, but the ! will cause it to be false. As soon as the parser sees that the left side is false, it stops because it know the entire expression will be false, and the right side will never be executed.

You might try:

Code: Select all

if (!(unlink($file1) && unlink($file2)) {
     echo 'files could not be deleted';
}
I think that will work.

Posted: Fri May 04, 2007 1:06 pm
by guitarlvr
It still did the same thing with the code posted. but i tried:

Code: Select all

if (!((unlink($file1)) && (unlink($file2)))){
     echo 'files could not be deleted';
}
and that seemed to work. Thanks blackbeard!

Wayne