As you can see in the screenshot I've changed my $CFG->tempdir to /private/var/www/moodledata/integration/2_3 which www-data does not have write permissions for. However the $CFG->dataroot is set to /home/gerry/Desktop/tempdata/moodledata/dev/MOODLE_22_STABLE which is owned and writeable by www-data. However the debug message tells me that dataroot is the problem, when in fact it's permissions are fine but tempdir isn't.
chmod o+w /private/var/www/moodledata/integration/2_3 #makes the debug message go away
chmod o-w /private/var/www/moodledata/integration/2_3; rm -fr /private/var/www/moodledata/integration/2_3/* #brings the error back
To replicate all you should need to do is set your $CFG->tempdir to a directory that isn't writeable by the user that your page runs under.