Forum Moderators: coopster

Message Too Old, No Replies

unlink not working

         

WhosAWhata

3:56 am on Jun 24, 2004 (gmt 0)

10+ Year Member



i have the following file

<?
if((!$activate) ¦¦ (!file_exists("engines/".$activate))){
$dir = "engines/";
$d = dir($dir);
while($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
$html .= "\n<p><a href=\"".$PHP_SELF."?activate=".$f."\">".basename($f,".php")."</p>";
}
}
echo "<html><head><title>Activate</title><style type=text/css>p { font-size:25pt; } a {text-decoration:none; border-bottom-color:0000ff; border-bottom-width:1; border-bottom-style:dotted; }</style></head><body link=blue alink=blue vlink=blue><h1 align=center>Activate which engine?</h1><hr width=85%><br>".$html;
} else {
$error = 0;
include("engines/$activate");
if($error) {
echo "failed";
} else {
if(@unlink("engines/$activate")) {
echo "Engine Successfully installed";
} else {
echo "Engine successfully installed. <b>Unable to delete temp files.</b>";
}
}
}
?>

it is used to install patches for a site when the user is ready

it works fine except that it won't delete the setup file and returns the error message the file has 0777 permissions why won't it delete?

ergophobe

3:12 pm on Jun 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The way you have your if statements, your error test will always be false. You set it to 0 and then in a nested if test it. You only test it in the block where you set it to zero. That means you never get the "failed" message, but always go to the "success" message. Since there may have been an error but you are not testing for it, it then fails on the unlink.

That may not be right, but you definitely need to fix that for starters.

If it still doesn't work, upon failure echo out the full path of the file you are trying to delete and make sure it is where you think it is.

Tom

WhosAWhata

3:45 am on Jun 25, 2004 (gmt 0)

10+ Year Member



how exactly should i do the if statements then? this was the best i can think of

ergophobe

2:43 pm on Jun 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




$error = 0;
include("engines/$activate");
if($error)

This is the problem. It means that your test for an error is always false. The problem is not with the if statement per se so much as the fact that your $error variable is meaningless. You statement is the equivalent of

if (false)

which will always evaluate to false. You have to put the $error=0 at the top of your script to initialize it, and then set it to something else if there is an error.

WhosAWhata

3:30 pm on Jun 25, 2004 (gmt 0)

10+ Year Member



i see what you are saying, but my include tests for an error...
examle include:


<?
if($file = fopen("somepage.php",w)){
fputs($file,"<? header(\"Location:somefolder/index.php\");?>");
fclose($file);
} else {
$error = 1;
}
?>

the problem is the unlink never works it always returns
Engine successfully installed. <b>Unable to delete temp files.</b>

ergophobe

4:05 pm on Jun 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And the actions from the included file are performed? If the include file was never found, you would still have an error of 0, and you would end up with the unlink problem.

By default, though, you would get a warning unless you have error reporting set to suppress warnings.

Why don't you try this. Right above the unlink, do this

$msg = (file_exists("engines/$activate"))? "<h1>File found</h1>" : "<h1>File NOT found</h1>";
echo $msg;

If the file is found and it still is not unlinking, perhaps it's a safe mode issue?

Tom

WhosAWhata

4:08 pm on Jun 25, 2004 (gmt 0)

10+ Year Member



i know i am not in safe mode (or a lot of other scripts would not work) and i know the file exists because the include executes the commands

so it isn't a matter of my coding?

WhosAWhata

4:16 pm on Jun 25, 2004 (gmt 0)

10+ Year Member



also if the file didn't exist wouldn't the top line catch it

if((!$activate) ¦¦ (!file_exists("engines/".$activate))){
//echo links
} else {
// include the file
}

ergophobe

4:43 pm on Jun 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry, I saw that the first time. Now I'm sort of brainstorming but just can't see it either.

Why are you suppressing any unlink error messages (with the @)? It might help to display it.

Tom

WhosAWhata

2:13 am on Jun 29, 2004 (gmt 0)

10+ Year Member



*bump*

coopster

7:14 pm on Jun 29, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




also if the file didn't exist wouldn't the top line catch it

if((!$activate) ¦¦ (!file_exists("engines/".$activate))){

Not necessarily, since you have an OR clause there in which you are also checking to see if there is no

$activate
variable. Try checking the file's existence again right before you
unlink
it (and take the error suppression off as ergophobe suggested until you have figured out the issue)...

ergophobe

6:15 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Right, but he says that

include("engines/$activate");

works. So then it should not be a problem of the file not existing in

unlink("engines/$activate");

I think there's some other problem and he needs to get rid of error suppression and see what's going on.

Tom

coopster

6:27 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure, but in the include I don't see any globalization of the $error variable, so setting it to boolean true without a

global $error;

in the function is not going to modify the global variable, thereby leaving the initialized $error variable at boolean false (zero).

ergophobe

8:43 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That was my first hypothesis, but there is no function, so the include just becomes part of the section it's included in, and therefore the value will trickle down.

Tom

WhosAWhata

6:41 pm on Jul 2, 2004 (gmt 0)

10+ Year Member



it was a problem with my host, they got it fixed
thanks for all your thoughts though
after none of your suggestions works i handed the problem to my support team and they found a flaw with the permission settings (when i set it to 0777 it got set back to 0775 when the control pannel accessed it)

sorry for all the unneccessary trouble

ergophobe

6:57 pm on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's a handy control panel feature!

If they just delete the file that would be even more secure ;-)

Tom