0

I'm trying to develop some webpages in my computer and it has been working fine until I tried to use fopen function, it returns FALSE eveytime. The .php file is located in the /var/www/html/ directory and the file I want to open is also in the same directory, but I also tried to open a file in /home/myusername/ directory and it doesn't work aswell.

I've seen in other post that it might be permissions and I did what was suggested and it doesn't work anyway.

My code:

    ...
$file = fopen("/home/msantos/direction.txt", w);
if ($file == FALSE) {
    echo "error fopen <br>";
}    
if(fwrite($file, "teste") == FALSE)
{
    echo "error fwrite";
}

And the result is always: "error fopen". And obviously since fopen didn't work I also get: "error fwrite".

Does anyone knows what I'm doing wrong or what I have to do to make it work?

EDIT: Has suggested in one of the comments I used error_get_last() and it outputs the following:

Array ( [type] => 2 [message] => fopen(/home/msantos/direction.txt): failed to open stream: Permission denied [file] => /var/www/html/teste.php [line] => 29 ) 

So it seems that it really is a permission problem. What do I have to do to make it work?

2
  • what distro are you using? on some distros you have selinux that block the read write permission of php and apache also show us the permission of the file Commented Dec 22, 2017 at 15:26
  • I updated the post with the detailed error. I'm using Fedora 26. Commented Dec 22, 2017 at 17:56

1 Answer 1

3

It's been a long time since I worked with PHP, but I believe fopen() needs to have a string/char parameter for mode. Thus:

$file = fopen("/home/msantos/direction.txt", w);  

becomes

$file = fopen("/home/msantos/direction.txt", 'w');  

Source

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.