Not working fopen() function in PHP (Apache, Linux Mint)
P粉722409996
P粉722409996 2023-08-13 14:14:43
0
1
430

fopen() does not work on my apache2 local server on Linux Mint

 The file 

test.txt is created and I can open it using Sublime and write to it. The directory for this particular project is /var/www/site02. 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").

If I access index.php using the code mentioned above, I get the following error:

"Warning: fopen(test.txt): Unable to open stream: Permission denied in /var/www/site02/index.php, line 6"

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粉722409996
P粉722409996

reply all (1)
P粉107772015

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 ismyusername, 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!

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!