fopen in php doesn't work

Posted April 4, 2018 10.9k views
ApachePHPUbuntu 16.04

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??

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.

Submit an Answer
2 answers

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().

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

  • Yes I am trying to save a new file. It does let me read and edit files that are already created but not create a file

  • The code was as simple as:

    $handle = fopen(‘empty.txt’, 'w’);

    And I logged in to another hosting provider siteground just to see if there was a difference and it worked fine there so I’m not sure if it’s some setup in my server or something.

    • The above code would actually not produce any output.

      $handle = fopen('empty.txt','w');
      This line opens the file empty.txt for writing. So you’re telling PHP you want to write inforamtion to the file.

      fclose($handle); This line closes the file. You didn’t write or read anything from the file named empty.txt.