I am using PHP file_put_contents
to generate PDF files inside a directory named invoices
.
This was working fine for a long time but stopped working with no changes to the code generating the invoices (I have asked the hosting providers about any changes to the installation but they are no help).
The error I get is "Warning: file_put_contents(test-generate-file/999999.pdf): failed to open stream: Permission denied"
However, this seems to be a file ownership issue rather than a permissions issue because after some testing I have found that:
The error occurs when the invoices folder has ownership
cpanelusername:cpanelusername
(this is the correct ownership needed for folders and files on the server to be viewed by visitors etc).Files are successfully generated when the ownership is changed to PHP which has the ownership name
nobody
(so ownershipnobody:cpanelusername
works)But the files that are generated successfully with ownership
nobody
can't be viewed by visitors.
As a workaround after they are generated I am manually changing their ownership to cpanelusername:cpanelusername
so they can be viewed. But this is a pain.
So the question is what is preventing files being generated by PHP when the folder has the correct ownership is in place and how can I fix it?
I have viewed multiple similar questions but they mainly refer to file permissions as opposed to file ownership and have not helped. Also setting permissions of the invoices
folder to 777 is not an option due to security being important.