fopen Permission denied, Ubuntu, CodeIgniter fopen Permission denied, Ubuntu, CodeIgniter codeigniter codeigniter

fopen Permission denied, Ubuntu, CodeIgniter


There are 2 ways :

1.) chmod www directory to 777 by this command "sudo chmod -R 777 /var/www".This will give permissions recursively to all sub-folders and files.

2.) If you create any new folders (or projects in www directory) in future after the 1st step , then follow above :

=> Right click on your current project folder=> Scroll to Properties=> Scroll to permissions=> Add permission for "Create and Delete files" for owner, group, others.=> Now click on "Change permission for enclosed files"=> Give appropriate permissions for files and folders there.

All done. Now fopen() should not give that warning.


  1. Open terminal
  2. Go to that directory which has the testFile.txt
  3. type: sudo chmod 755 testFile.txt (or use 777 for writing)

To create a file with terminal:

sudo touch testFile.txt

To create a file with php if not exists:

fopen('testFile.txt', 'w');

Note: If you are not the owner that folder you can not write.

Be sure to parent folder / directory has at least 755 chmod.


I think you need to change the permission of the file you are trying to open. Also you didn't mentioned whether you want to read or write to the file. In general you can give permissions to the file as below:

chmod 777 testFile.txt

Or In case you want full www directory and its contents to have all permissions recursively, try this:

sudo chmod -R 777 www/

After doing the above you should be able to open the file, as below code does:

$handle = fopen("testFile.txt", "r"); // for reading

or

$handle = fopen("testFile.txt", "w"); // for writing

Change your function as below:

public function create_file(){$ourFileName = 'testFile.txt';$ourFileHandle = fopen($ourFileName, "w") or die ("can't open file");fclose($ourFileHandle);

}