Home  >  Q&A  >  body text

Not working fopen() function in PHP (Apache, Linux Mint)

<p>fopen() does not work on my apache2 local server on Linux Mint</p> <pre class="brush:php;toolbar:false;"><?php error_reporting(E_ALL); ini_set('display_errors',1); include_once "c&f.php"; fopen("test.txt", 'w');</pre> The file <p>test.txt</code> is created and I can open it using Sublime and write to it. The directory for this particular project is <code>/var/www/site02</code>. When configuring apache2 after installation, I specifically changed the ownership of this specific directory (site02) to be able to read and write files (sudo chown $USER:$USER -R ... you get the idea). In fact, I can indeed read and write with my user account in this directory (I can freely use cli git commands in this directory without "sudo"). </p> <p>If I access <code>index.php</code> using the code mentioned above, I get the following error: </p> <blockquote> <p>"Warning: fopen(test.txt): Unable to open stream: Permission denied in /var/www/site02/index.php, line 6"</p> </blockquote> <p>What happened? I've tried googling the problem but got thousands of different reasons, which only confused me more. Is the problem a problem with the apache server? Or do I need to make some configuration changes to PHP? </p>
P粉722409996P粉722409996403 days ago464

reply all(1)I'll reply

  • P粉107772015

    P粉1077720152023-08-14 11:21:06

    So, after posting this question, I decided to open /var/www/site02 with a file manager (Thunar) to check the permissions under the graphical interface. Yes, the owner of this directory is me and I can read and write. The group is myusername, and the permissions are also read and write. But for "others" there is only one permission: read. I changed it to read and write. And test it with some code:

    $fh = fopen("test.txt", 'w') or die("error writing the file");
    
        $text = <<<_END
        line 1
        line 2
        line 3
        _END;
    
        fwrite($fh, $text) or die("error writing the file");
        fclose($fh);
        echo "成功!";

    And it worked!

    reply
    0
  • Cancelreply