3

I have a PHP script that writes some rows to a CSV file:

$fp = fopen($csv, 'w');

I'm using mode 'w' so that it will create the file if it doesn't exist, however the file is automatically given 644 permissions even though I gave 777 to the whole /var/www/html/ directory (not a good idea but suitable for testing).

How can I write to the file and give it permissions beforehand? Or is there a better way to give write permissions?

AlxVallejo
  • 3,066
  • 6
  • 50
  • 74
  • are you having trouble writing to the file? 644 should be fine for writing as the owner has read/write permissions. – Matt K Aug 15 '12 at 14:18
  • @MattK It seems to create the csv under the owner of 'Apache' which is different than the owner of all the other files in the directory – AlxVallejo Aug 16 '12 at 20:53

1 Answers1

6

To create the file before hand and set permissions:

touch('/file/path/here');
chmod('/file/path/here', 0775);
fopen('/file/path/here', 'w');

But, this should not be an issue. If the PHP/Apache user is creating the file using fopen, they have permissions to write to it.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87