msantos
msantos

Reputation: 3

PHP fopen function not working

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?

Upvotes: 0

Views: 2476

Answers (1)

3d12
3d12

Reputation: 133

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

Upvotes: 3

Related Questions