How can I create txt file using php apache?

I’m trying to create txt file on fly using php fopen but unable to create it for unknown reason.

Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Site Moderator
Site Moderator badge
February 12, 2023

Hello @pranavkiritkumarsuchak

Just to add on what has already been mentioned, you can also check for any generated PHP error_log files in the directory where the .txt file is supposed to be created. This might point in the right direction as well.


Site Moderator
Site Moderator badge
February 12, 2023

Hey @pranavkiritkumarsuchak,

I’m sure the problem is with permissions. Apart from what Bobby has already mentioned I’ll recommend you to check the SELINUX of your Ubuntu.

You can check this tutorial for more information:

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Bobby Iliev
Site Moderator
Site Moderator badge
February 10, 2023

Hi there,

You would need to make sure that the Apache user has access to create files in your web root directory.

By default Apache runs as the www-data user so you could use: sudo chown www-data /var/www/html to change the owner of the directory where you want to create your file at.

If this still does not work, I would recommend checking your Apache error log for mre infomration on why this is failing:

tail -100 /var/log/apache2/error.log

Let me know how it goes!