Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function.fopen: failed to open stream: Permission denied in PHP

I'm trying to create XML sitemaps for my website from my PHP application. The idea is to either create a new file or overwrite an existing file. When I call fopen, I get the following error:

[function.fopen]: failed to open stream: Permission denied

I'm trying to write to the webroot and its permissions are: 755. This means that the owner has write permission, right? What do I need to do to make my script be able to write to this folder? 777 would be a bad thing, right? Can I run my script as owner somehow?

Thanks.

like image 739
StackOverflowNewbie Avatar asked Mar 09 '11 23:03

StackOverflowNewbie


1 Answers

Yep, as you've said, using 777 could be huge mistake. The webserver doesn't run with the same user as you use to create files and folders.

You have some options:

  • Run the sitemap creation as a cronjob, using an user with rights to write there, other than the apache user.
  • Put the sitemap in another directory, and the set up a 302 Redirect or a symlink. In this case, if you have a security issue that let's someone to write your sitemap.xml, at least they'll not be able to create another file with a more dangerous extensions (like PHP, which may result in a site intrusion).
  • Make a rewrite rule to redirect any hit to sitemap.xml, to a php script that outputs the appropriate XML.

Good luck!

like image 115
Gonzalo Larralde Avatar answered Oct 13 '22 00:10

Gonzalo Larralde