Hi. I have a noobish UNIX question.
I have a password-protected section of my website for testing (named 'sandbox') and I'm trying to write output to a plain text file in that directory. But I'm getting the following error due to permissions settings.

Warning: file_put_contents(testdb.txt) [function.file-put-contents]: failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/sandbox/dbtest1.php on line 5

I'm sure it's something simple, but what could I be overlooking?

Dani AI

Generated

Short answer: the PHP process that writes the file needs filesystem ownership or the appropriate write bits on the directory (write + execute on the directory). What meant by “attributes” is the owner/group and the rwx bits; “owned by the writing process” means the user account the web server/PHP runs as must be allowed to write there. The quickest way to see the problem is to inspect owner/group and perms for the sandbox and the file.

Check who owns the files and what the webserver runs as:

ls -ld /Applications/XAMPP/xamppfiles/htdocs/sandbox
ls -l /Applications/XAMPP/xamppfiles/htdocs/sandbox/testdb.txt
ps aux | egrep '(httpd|apache|apache2|php-fpm)'

Avoid leaving the directory world-writable. Instead of 777, set the directory so the webserver user (example names: www-data, apache, _www, nobody) is either the owner or in the group, then tighten perms. Example workflow (replace names with the ones you discovered above):

sudo chown -R youruser:www-data /Applications/XAMPP/xamppfiles/htdocs/sandbox
sudo chmod g+s /Applications/XAMPP/xamppfiles/htdocs/sandbox
sudo find /Applications/XAMPP/xamppfiles/htdocs/sandbox -type d -exec chmod 750 {} \;
sudo find /Applications/XAMPP/xamppfiles/htdocs/sandbox -type f -exec chmod 640 {} \;
sudo chmod 600 /Applications/XAMPP/xamppfiles/htdocs/sandbox/.htpasswd

This gives the webserver group access without exposing the directory to every local user. As warned, world-writable is risky.

If it still fails, confirm PHP’s effective user (a tiny test script that runs whoami), check phpinfo() for open_basedir restrictions, and, on SELinux-enabled systems, verify file contexts. After fixing ownership/permissions, remove any 777 you set so .htaccess/.htpasswd and other test files stay protected.

Recommended Answers

All 5 Replies

It is likely that the attributes on the directory in question don't allow this. You need to enable the read, write, and execute permissions accordingly, assuming that the directory is not owned by the writing process(es).

Thank you.
What does it mean for a directory to be owned by the writing process(es)?

I just did a chmod 777 on the directory and that fixed everything. :-)

I was concerned that doing so might also recursively change the permissions of the directory's contents (including my .htaccess and .htpasswd files) but it did not.

While that may not have chnaged things recursively, your original claim of an isolated location is now invalid. That directory is open to all for reading/writing/executing. I'm not sure that matters in your case but you should be aware.

What L7Sqr said. The attributes you set on the directory means that anyone can, read, write, and access, files there. That may or may not be what you really wanted to do. It doesn't mean that they can modify files that already exist there, though that may be the situation depending upon the user id of the web server process.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.