Report this

What is the reason for this report?

fopen in php doesn't work

Posted on April 4, 2018

Hello! When I try a simple fopen(“hello.txt”, “w”) it doesn’t do anything at all. I changed to full permissions 777 and changed around the owner to root and different ones and nothing. I added var_dump and echoed the fopen and it gives me False(bool). I checked the fopen configuration in the php.ini file and it is enabled. Any help??



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!

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.

What are you trying to do?

The “w” in fopen(“hello.txt”,“w”) means to write to a file, not read. It would be best to read the PHP documentation on fopen().

http://php.net/manual/en/function.fopen.php

Lots of examples! Hope that helps, let me know if I can help more.

This comment has been deleted

1. Check open_basedir Restriction:

If open_basedir is set in php.ini, it restricts PHP from accessing files outside of specified directories. Check if it’s enabled:

php -i | grep open_basedir

If it’s set, either disable it by removing or commenting the open_basedir directive in php.ini, or add the path to your directory:

open_basedir = /path/to/your/allowed/directories

2. Verify Write Permissions for PHP’s User:

Make sure the user under which your web server (likely www-data or apache) runs has write permissions to the directory where hello.txt is being created.

To check the current user running the web server:

ps aux | grep apache  # or httpd on CentOS

Then, ensure that the directory where you’re trying to create the file has the correct ownership and permissions:

sudo chown www-data:www-data /path/to/your/directory
sudo chmod 755 /path/to/your/directory

3. Check for Disk Space and Quotas:

Sometimes, the failure could be caused by a lack of disk space or quota issues. Check the available disk space:

df -h

4. Check SELinux (if applicable):

If you’re running a system like CentOS with SELinux enabled, SELinux could be preventing PHP from writing to certain directories. Temporarily disable SELinux to check if it’s causing the issue:

sudo setenforce 0

If this fixes the problem, you can either adjust the SELinux policies or leave it in permissive mode.

5. Check Error Logs:

Check the PHP or Apache error logs for more detailed information about the failure. You can find logs in the following locations:

tail -f /var/log/apache2/error.log  # For Apache on Ubuntu/Debian
tail -f /var/log/httpd/error_log    # For Apache on CentOS/RedHat

Look for any relevant errors related to file permissions or fopen.

6. Check disable_functions in php.ini:

Ensure that fopen is not disabled in the php.ini file under the disable_functions directive. Check if it’s listed:

php -i | grep disable_functions

If fopen is listed, remove it from the disable_functions directive and restart your web server:

sudo systemctl restart apache2  # Or php-fpm depending on your setup

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.