Jump to content

[SOLVED] Can't write to a file on local server


owstopit

Recommended Posts

Hello!

 

First of all I apologize if this has already been answered and I have looked. I am very new to php and while I don't have much trouble with writing php, I am very unfamiliar with servers and how they work. I just started learning php and I've been working out of the friends of ed book PHP Solutions. I have been using Apache on Mac OSX as a test server.

 

Right now I'm working with a simple piece of php that basically just takes what is written into a text field and writes it to a txt file using the fopen_write function. Whenever I run the script I get the [function.fopen]: failed to open stream: Permission denied error. I have read that folder permissions and file permissions can be the cause of this. I tried changing the permissions to read + write for both the file and folder but still nothing. I also read on the php website that sometimes this happens because you need to change the system account in Apache. I do not know how to change the system account, nor do I know how to fix this problem! Please help! Thanks so much for your time.

 

-Neal

Link to comment
Share on other sites

try this

$filename="$_POST[title]";

if (!$handle = fopen($filename, 'a')) {

        echo "Cannot open file ($filename)";

        exit;

  }

 

  if (fwrite($handle, $body) === FALSE) {

      echo "Cannot write to file ($filename)";

      exit;

  }

that is what i use well close to it i change it a little the file name is the full path on my server like C:/drive/location i hope this helps

 

 

 

  fclose($handle);

 

 

Link to comment
Share on other sites

chmod is the file permissions.

 

grab your favorite ftp client and connect to your server. I use coreFTP free edition. Go to the file and right click. choose properties and then set them for

 

user                group          world

 

read                  read            read

write                execute      execute

execute

 

This is 755

 

Read is worth 1, write is worth 2 and execute is worth 4, so user 4+2+1=7, group  4+1=5 , world 4+1=5

 

Nate

Link to comment
Share on other sites

Ok,

 

the only problem now is that I'm not doing this on my domain server, I'm just doing it on my local server. How do I connect to my local server (the one setup by apache) in an ftp program? I tried just entering the ip address but that doesn't seem to work. As you can see I am pretty n00bular when it comes to this kind of stuff but even knowing that I can set that file permission with an ftp client is helpful to me!

 

So, how can I connect to my local server through an ftp client?

Link to comment
Share on other sites

Hm, well I went to the folder properties and set them to read and write for my account (the main account on Mac OSX) but there are many possible accounts to grant permission to. Maybe I'm granting permissions to the wrong one? I don't have time to right at this moment, but I will post the code, as perhaps there is a problem with my script?

 

Until then, does anyone know what account I need to grant permission to? Here are some of the possible accounts I can grant permission to:

 

-accessibility

-admin

-amavisd

-appowner

 

and many more in alphabetical order.

 

Hmm, just not sure what to do here, I'll post the code later today. Thanks for the help so far, I'm sure we can figure this out!

 

thanks

-Neal

Link to comment
Share on other sites

Well, I fixed it!  ;D

 

Turns out Apache is running under user "www". What confused me is that I had already given the folder permission for "other" which includes all of the accounts, but hadn't actually applied it to the folder! So, don't make the same mistake I did: if you're changing folder permissions in OSX always press "apply to enclosed items" when you're done!

 

I fixed it by applying permission to every account, then decided I wanted to know which one it was specifically and after trying a few that seemed likely it turned out that 'www' was the right account. So I guess it's useful to know that for users of OS X 10.4.11 (tiger), apache may very well use account 'www'.

 

Thanks so much for the help everyone!

 

-Neal

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.