I have a PHP script that is supposed to save images to /var/www/backend/images/ folder.The script is in /var/www/html/save_image.php

The owner and the group of all the folders and files in /var/www/ is ubuntu.

For testing, this is my simple script that won"t work:

And I get the following message:

Warning: fopen(newfile.txt): failed to open stream: Permission deniedin /var/www/html/save_image.php on line 2 Unable to open file!

How can I allow this script only to save files?

You are watching: Fopen failed to open stream permission denied


Silly question, but do you mean the PHP script is going to save files to /var/www/backend/images? Otherwise, if you want to save to a location outside of the /var/www location, you'll need to make some changes to Apache's AppArmor configuration so the web server has permission to write to a different location.

See more: Wearable Lanterns Read/Write Error Please Help, Frostfall Se With Skse64

Next question would be: is the ubuntu account configured as the owner of the Apache process? Generally — but not always — Apache is run with the www-data account. This would mean that /var/www and any subdirectories would need to be owned or writable by that account.
Ah, EC2. That explains the account name. Then you’ll want to do this: sudo chown -R www-data:www-data /var/www. Unless you specifically changed your Apache config to use the default user account, then Apache is using www-data