file_put_contents - failed to open stream: Permission denied

Solution 1:

Try adjusting the directory permissions.

from a terminal, run chmod 777 database (from the directory that contains the database folder)

apache and nobody will have access to this directory if it is chmodd'ed correctly.

The other thing to do is echo "getcwd()". This will show you the current directory, and if this isn't '/something.../database/' then you'll need to change 'query.txt' to the full path for your server.

Solution 2:

You can make Apache (www-data), the owner of the folder:

sudo chown -R www-data:www-data /var/www

that should make file_put_contents work now. But for more security you better also set the permissions like below:

find /var/www -type d -print0 | xargs -0 chmod 0755 # folder
find /var/www -type f -print0 | xargs -0 chmod 0644 # files
  • change /var/www to the root folder of your php files

Solution 3:

There's no need to manually write queries to a file like this. MySQL has logging support built in, you just need to enable it within your dev environment.

Take a look at the documentation for the 'general query log'.