fopen Permission denied, Ubuntu, CodeIgniter

18,903

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.

Share:
18,903
Lupita Noyra
Author by

Lupita Noyra

I'm really newbie and need to learn more.

Updated on June 14, 2022

Comments

  • Lupita Noyra
    Lupita Noyra almost 2 years

    I was trying fopen for my project. Which running on Ubuntu with CodeIgniter PHP framework. The result was like this:

    A PHP Error was encountered
    Severity: Warning
    Message: fopen(testFile.txt): failed to open stream: Permission denied
    Filename: controllers/home.php
    Line Number: 10
    
    can't open file
    

    By the way, here are my code on Controller:

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

    I looked at that code, and I thought that there is should be a path where I open the file, right? If yes, where should I put the path? Because, I followed this code from this tutorial: http://www.tizag.com/phpT/filecreate.php.

    One thing that make me confuse is, there is no file testFile.txt and I wanna create it, but how to give the permission to create it. Because terminal will say No such file or directory for the file. What should I do?

    I've also tried to run the chmod on www directory. But, it still didn't work. Hope anyone help me to handle this problem. Thank you...

  • Lupita Noyra
    Lupita Noyra almost 10 years
    Thank you for your advice, Hari. I've tried it, but when I clicked on "Change Permission for enclosed files" the file access turned to none (--). what should I do?
  • Lupita Noyra
    Lupita Noyra almost 10 years
    Thank you for your advice, Canser. I've tried it, but still doesn't work. One thing that make me confuse is, there is no file testFile.txt and I wanna create it, but how to give the permission to create it. Because terminal will say "No such file or directory for the file". What should I do?
  • Lupita Noyra
    Lupita Noyra almost 10 years
    Hi, mSatyam. Thanks for your advice. I've tried it but still doesn't work. One thing that make me confuse is, there is no file testFile.txt and I wanna create it, but how to give the permission to create it. Because terminal will say "No such file or directory for the file". What should I do? thank you.
  • Canser Yanbakan
    Canser Yanbakan almost 10 years
    You are searching that file on the same directory that your .php file. Do you realize that? So you have to go to that directory which your .php file placed, and create a file in that directory.
  • Hari
    Hari almost 10 years
    Hello Nana, First of all chmod www directory to 777 by this command "sudo chmod -R 777 /var/www" , after that follow the above process. My answer modified.
  • mSatyam
    mSatyam almost 10 years
    if the file is not already created you should open it with write mode, which creates the file if not already created. $handle = fopen("testFile.txt", "w"); // for writing. I have edited my post including changes need to be done in your code. One more thing if file is not already present why you what to read from it. One will only read from a file which exists.