Cannot write to file with fopen() , Permission denied

I get failed to open stream: Permission denied when trying to create a file, or write to a file (if I manually create it first in FTP or SSH).

I’ve looked at many aspects but I don’t find what may be the issue. So maybe it could related to wordpress in some way. The wordress setup is based on Bedrock (with a different folder structure).

Here are the details:

I do an ajax call from the frontend, which reaches a method in the backend, inside a plugin script. The method is an ajax function which is registered with a "wp_ajax_nopriv" action.

Folder path: This script is in /mnt/persist/www/

Owner: The "src" folder and all its parent folders have a certain user, "deploy" as owner.

Permissions: All folders have 755. The file: If I create it manually, I have set it to ‘775’ and deploy as owner.

Current user: If I do get_current_user() inside the ajax method, I get "deploy".

Working directory: If I run getcwd() inside the script however, the working directory turns out to be: /mnt/persist/www/

I changed permissions in "src" to 777.

I use "fopen" like this:

$  fp = fopen( $  folderPath . $  filename . '.txt' ,"a" );        if(!$  fp){     die('Error: ' . implode(',',error_get_last())); } 

If I try to create/write to a file in the current directory (not using a folder path), it doesn’t work, and if I try to use the path: /mnt/persist/www/ (which is verified with file_exists), plus the filename, it gives the same Permission denied error.

If I SSH into the server and change user to deploy, and go into "src" folder, I can create a file with "touch myfile.txt".

Concerning the working directory ( /mnt/persist/www/ , those folders also have "deploy" as the owner, and permissions set to 755.

Can I trust get_current_user(), which says deploy? I thought I might be running it as www-data, but since it says deploy, there shouldn’t be any problem?

I’m a bit clueless..