Php How Do I Use Fopen() While Being Able to Edit Files in the Website Directory as Non-Root User?

I'm running a local apache2 web server at /var/www/.

In order to use the fopen() function in php, the user and group of my site directory must both be 'www-data'. Problem is, when I try to edit files as normal user in the website directory, the normal user has insufficient permissions to write.

1 Answer

To enable both your ordinary user and the php interpreter to read and write the same files - and assuming the www-data group already has read + write permissions on the files - you can add your user to the www-data group.

sudo gpasswd -a david www-data

This won't take effect until you logout and log back in again. Until then, you will be able "spoof" being in the www-data group with:

newgrp www-data

This will cause your shell to fork into a newgrp process, during which you'll have a new shell session with belonging to www-data group.
You can exit this with Ctrl+D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest