Context
- Apache 2.4.18
- Ubuntu 16.04.1
- PHP 7.0.8
- WordPress 4.6 (This may be irrelevant as this question is mainly just a PHP writing thing, but I am working within a WordPress theme's directory.)
Files
I have two files: foo.php and bar.css.
This is foo.php (located in /wp-content/themes/theme-name/):
<?php
// Write some stuff to bar.css
file_put_contents('abs/path/to/bar.css', 'Blah blah blah...');
?>
and bar.css is completely blank (also located in /wp-content/themes/theme-name/).
Goal
I'd like to allow foo.php to write to bar.css without me having to up the permissions of the bar.css file to 666 (from 644). I've tested it, and I can get it to work just fine if I change the permissions of bar.css to 666, but again, that's something I'd like avoid.
What I've tried
I know this might seem like a redundant question, but honestly, I've been looking on SO and beyond, and I still haven't found anything that fixes this. I mean, I've read around (such as this thread or that thread, etc.) and maybe there's something I'm just not getting, but to my understanding based on things I've read is once I change the permissions to at least 644 and set the file owner to www-data:www-data, PHP should be able to write to it and life should be good. But... that's not happening.
The theme editor in the WordPress admin panel (example.com/wp-admin/theme-editor.php?file=bar.css&theme=theme-name) is able to write to bar.css even when it's at 644... so what's the difference between that and what I'm trying to do? What could I be doing wrong?
Thanks for any help.