The simplest place to write work files with PHP under Apache if privacy doesn't matter is /tmp. Files written to /tmp will be deleted if the system is rebooted, and they can be read by all users on the system. If they are truly temporary files, you should probably delete them at the end of your script. Give the files names that won't cause a collision like this:
If you need a place to write persistent, private files, you need to create a directory that is writable by the www-data user and group. For maximum privacy use 700 permissions on the directory, and chmod() the file to 600 after running fclose().
exec("rm -f $tempfile");
If you want to create files that you can read and modify, add yourself to the www-data group and use 770 and 660 permissions on the directory and files respectively rather than 700 and 600. To add yourself to the group, edit (as root with sudo) the file /etc/group and add your username after the trailing colon in the line that begins "www-data". Or use the GUI users and groups management tool in Ubuntu.
sudo mkdir /path/to/new/directory
sudo chown www-data.www-data /path/to/new/directory
sudo chmod 700 /path/to/new/directory