How should I go about with writing files that should not be accessible to the public? First how can I write to file outside of the CI root folder? Second to be able to write file, I need to set the file permission to 777, which then would become accessible to the public. How should I approach this?
Asked
Active
Viewed 259 times
0
-
First of all, you have to get permissions to set thw owner of your server files. Take a look here: http://serverfault.com/questions/357108/what-permissions-should-my-website-files-folders-have-on-a-linux-webserver and the use fopen, fwrite and fclose normaly. – giordanolima Oct 07 '14 at 14:43
1 Answers
0
The CI File Helper will "help" you do this:
$this->load->helper('file');
write_file('./path/to/file.php', $data);
The path to the file can be a relative path or an absolute path, so you can write this file anywhere on the system (even above the CI root folder - as long as you have permissions to write to the directory.
For example:
// CI Root folder is at /var/www/my_ci_project
// Directory to write to is at /var/www/tmp_files
$this->load->helper('file');
write_file('/var/www/tmp_files/file.php', $data);
As for the permissions, you just need the file to be group writable by the webserver user:
// Everything for owner, read and execute for owner's group
chmod("/var/www/tmp_files/file.php", 0770);

swatkins
- 13,530
- 4
- 46
- 78