可能没有正确调用函数

I posted an issue earlier, and this most likely has something to do with it.

I try to fwrite() to a file right next to index.html, but it isn't working. I have set so everyone has all permissions.

Code:

<?php
if(isset($_GET['button1'])){test();}
function test()
{
    $f = fopen("test.txt", "w+");

fwrite($f, "Hello World");

fclose($f);
}
?>

<html><body>
<h1>Hello World!</h1>
<input type=button onClick='location.href="?button1"' value='Toggle Lamp'>
</body></html>

Is it not calling the test() function properly?

EDIT: Whoops, that's not the entire code. Updated, sorry!

EDIT2: Doesn't work by calling just test(); either. Seems as if something is wrong in that function.

EDIT3: FIXED! Updated code to fixed code so others can see.

Try if(isset($_GET['button1'])) { test(); }

As it stands, I believe you are implicitly asking it to see if $_GET['button1'] == true which is not what you have set in your <a href="..."> tag in your HTML.

Your fopen call is likely incorrect. You should give it the complete path to the file to write. If you're on apache you should be able to get the webroot path from $_SERVER['document_root'] or another environment variable. Or, if its really in the same directory as index.php use __DIR__ constant:

$f = fopen(__DIR__ . "/test.txt", "w+");