PHP fopen can't create a file

19,628

Solution 1

If you use the terminal and go to the parent of folder your file will be created in, which is the parent of the include folder and type in the command:

chmod 777 include

This should change the permissions of this folder so you won't receive the permission denied error anymore. If you do try this command:

chmod -R 777 include

Solution 2

I think you might be ignoring the permissions of the directory (./include).

I'm assuming you are running this PHP via a web-server and on Linux (like Apache for example) - in which case the user account that is trying to create the file will be 'apache' or 'www-data' (or whatever user your webserver is running under).

On your server - have a look at the permissions of ./include - you need to do one of two things:

a) make ./include world writable (so the 'apache' user can now create a file inside of it).

b) change the owner or group of the ./include to 'apache' so it can create a file inside of it.

Your PHP is fine - it's the permissions of the folder it is trying to create the file inside of that is not.

Share:
19,628

Related videos on Youtube

Rox
Author by

Rox

Dreamer, full of fantasy, realistic. My duty of life is: programming, security, happiness.

Updated on June 04, 2022

Comments

  • Rox
    Rox almost 2 years

    I have a function in PHP language to create an xml file when requested.

    if(($file= fopen("./include/catalogo.xml", "w"))==false){echo 'non creo il file'; return false;}
    

    "catalogo.xml" can't be created, permission denied. I know I should try to change permissions, but how can do this if the file doesn't exist? Or, are there things that I ignored?

    • Rox
      Rox almost 12 years
      I use OS X Lion, I'm working on my computer using XAMPP.
  • Anachronist
    Anachronist over 10 years
    That's actually a poor answer. You shouldn't be giving world-write access to any directory in a directory tree accessible to the web. I came across this entry because I am trying to solve this problem myself, but there's no way I'm going to chmod 777 on a directory under the current one. Any web directory should be chmod 711, and I believe it should be possible for php to create files in it, if you assign the proper permitted group to the apache 'user'.
  • Ivan Buttinoni
    Ivan Buttinoni over 10 years
    Well, I can understand your point 1777 is quite wide open permission, but what's the real advantages of your permission? Why not 700 but 711? I really think that your comment can be an interesting question.