-1

fopen() doesn't work on my apache2 local server on linux mint

<?php
    error_reporting(E_ALL);
    ini_set('display_errors',1);
    include_once "c&f.php";

    fopen("test.txt", 'w');

File test.txt is created and I can open it and write something to it using Sublime. Directory for this particular project is /var/www/site02. While I was configuring apache2 after installation, I specifically changed the ownership of this particular directory (site02) to be able to read and write files (sudo chown $USER:$USER -R ... you get the idea). And I indeed can read and write from my user account in this directory (I can use cli git commands in this directory freely without "sudo").

If I access the index.php with the code I mentioned above, I get this error:

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

What is going on? I've tried to google this issue but I get million different reasons which only confuse me more. Is the problem with apache server? Or I need to do some config changes for PHP?

brombeer
  • 8,716
  • 5
  • 21
  • 27

1 Answers1

-1

WELL, WHAT A GENIUS I AM. I mean, sorry. So, after posting this question I decided to open /var/www/site02 with file manager (Thunar) to check the permissions with gui. Yes, the owner of this directory is me, and I can read&write in it. The groupp is NAME OF MY USER and permissions is r&w too. But for "Others" there was only one permission: read. I've 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 "Succses!";

AND IT WORKED!

  • 2
    What a genius! You have now allowed every hacker in the universe potential access to your website directories – RiggsFolly Aug 12 '23 at 13:16
  • 1
    Your server is running using an account like `www` and not as YOU. And it should never be running as you. – RiggsFolly Aug 12 '23 at 13:18
  • @RiggsFolly this is a local machine where I test and learn stuff. Did I say something about deployment, or how good is this for deployment in particular? No. So what is the problem with your attitude? – bounty_python Aug 12 '23 at 14:11
  • 1
    Well basically, it is always best to do things Right First Time rather than labour under the misapprehension that you got it working once so you can do that again. **Learning Stuff machine, so learn how to do it right, makes sense right?** I assume this PC is or will at some time get connected to the internet. All it takes is an inexperienced admin(you) to badly configure an Apache or not update it or not to activate a security module they didnt think they would need on my playtime maching and you will be back with a sad story of a destroyed development you almost finished after being hacked – RiggsFolly Aug 12 '23 at 14:22