Unable to Create/Write File due to Permissions

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

Post Reply
webbiz
Forum Newbie
Posts: 22
Joined: Sat Mar 20, 2010 12:04 am

Unable to Create/Write File due to Permissions

Post by webbiz »

Hello. I'm an absolute PHP newbie. Just to get that out of the way. :)

On my site, I have a file called .htaccess. When someone drops into my site at the URL where this file exists, it pops up a dialog box that asks a question (I use the AuthName for this) for the user to look up the answer to discover the password.

I want to change this question everyday. So I wrote a PHP script that has the questions in an array and selects the questions based on what day it is. So far, so good.

My problem, however, is that I am unable to write to this file. I get a Permission Denied type error message. So I tried to use the Chmod() to change the file to 0777. No good. It comes back "Operation not permitted..." and the line number of the Chmod().

I'm just not sure where to go from here. I want to write over (not append) this file each time the script is run. What am I doing wrong?

Thanks.
Webbiz
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Unable to Create/Write File due to Permissions

Post by Christopher »

How did you create the .htaccess file in the first place? It probably has the user set to your login/FTP account -- not the webserver. PHP runs as the webserver user, so it cannot change the permissions (which is a good thing). You can change the user for .htaccess to be the same as the webserver. However, it might be better to write you own authentication code rather than using HTTP authentication.
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Unable to Create/Write File due to Permissions

Post by Benjamin »

webbiz wrote:So I tried to use the Chmod() to change the file to 0777. No good. It comes back "Operation not permitted..." and the line number of the Chmod().
Have you tried changing the permissions via FTP or SSH?
Christopher wrote:However, it might be better to write you own authentication code rather than using HTTP authentication.
Chris is right, the way you are trying to do this is not correct because there are better and more secure ways to accomplish the same functionality.
webbiz
Forum Newbie
Posts: 22
Joined: Sat Mar 20, 2010 12:04 am

Re: Unable to Create/Write File due to Permissions

Post by webbiz »

What better solutions are there? I'm open to direction on this.

As long as I can get it to where if someone tries to access this directory they have to answer correctly a question asked, that would fit the bill.

:)
Webbiz
jmgek
Forum Newbie
Posts: 2
Joined: Sat Sep 04, 2010 11:35 pm

Re: Unable to Create/Write File due to Permissions

Post by jmgek »

Try 755 I think in Cmod
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Unable to Create/Write File due to Permissions

Post by kaisellgren »

I ask for FTP credentials just as I ask for SQL credentials. Things are more likely to work using the user's FTP account. Try that.
Post Reply