posix_setuid(1500) only changes the UID to 1500. That does not give you permission to access the file. The reason you can access the file when logged in as your regular user is that you not only have UID 1500 but also group 1500 and supplementary groups 27 and 33. Having supplementary group 33 (
www-data) is the thing that causes you to have access to the file.
You should probably be doing this:
The order is important: after you give up root permission by calling
posix_setuid you can't set your credentials anymore, so it wouldn't work in the other order.
Note that the documentation for
posix_initgroups is probably wrong (like many things in the PHP world). It says it "Calculates the group access list" but I guess that it actually behaves like the POSIX function that it references, which, if true, would mean it doesn't just calculate it but actually sets it.