PHP will already be able to write text files.
PHP code is executed by the server; Apache.
Apache should be configured to run as a non-root user. Sometimes this user is 'www' or 'wwwrun' or 'http' etc. depending on your distro. You can check what user Apache is running as by looking at the running Apache process:
$ ps -ef | grep -i http
The user running the server will be in the first column, let's say it's 'wwwrun'. You can see info about this user using 'id':
$ id wwwrun
uid=30(wwwrun) gid=8(www) groups=8(www)
This shows us that the user is 'wwwrun' and is in the group 'www'. You can use this information to create a 'safe' place for your text file to be written:
$ mkdir /some/path/to/a/dedicated/directory
$ chown wwwrun:www /some/path/to/a/dedicated/directory
$ chmod 750 /some/path/to/a/dedicated/directory
Now your PHP application can create files in '/some/path/to/a/dedicated/directory' and only the Apache user can read/write them.
If your cron/batch process needs to edit/read these files, create a new group 'mygroup' (or whatever) add your user to 'mygroup' and chgrp the /some/path/to/a/dedicated/directory to 'mygroup'.
Hope this helps!