0

Im trying to write to file using php but it doesn't see to work.

$file = fopen("test.txt","w");
    fwrite($file,"Hello World!");
   fclose("$file");

I published the site using the school server. and the php file/text file are all in public_html folder. The problem is, running this into terminal writes the file. Running it as a website doesn't.

How do i fix this?

P.S. I've tried chmod 644, 777, 755

user2055171
  • 61
  • 3
  • 10

2 Answers2

1

Remove the quotes in fclose :

$file = fopen("test.txt","w");
fwrite($file,"Hello World!");
fclose($file);

Also, you have to grant all permission : see that answer.

Community
  • 1
  • 1
Gabriel L.
  • 4,678
  • 5
  • 25
  • 34
0

Hmm... is it a UNIX/Linux server? You probably want to give the group www-data permissions to write the folder.

Assuming your web folder is /var/www, try doing this:

sudo chown `whoami`:www-data /var/www
sudo chmod 775 /var/www

And try again. If you're working in your personal web folder (http://domain.ext/~yourusername), the commands should be:

chown `whoami`:www-data ~/public_html
chmod 775 ~/public_html

Does it work running the script now?

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30