/test.txt
would be a file in the ROOT directory of your filesystem, where user accounts generally do NOT have write privileges (unless you're running this code as root). This is especially true of PHP running under the webserver's user account.
You probably want just test.txt
(no leading slash)` which will try to put the file into the script's "current working directory" - usually the same directory the script itself is in.