简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM