Page 1 of 1

PHP Writing to a file

Posted: Fri Nov 11, 2011 10:45 am
by ajtgarber
I'm creating a test site so that you enter a name and a message, and once you submit the form it appends what you added to a file, the main page reads from the file and writes it to the page. For some reason the reading works fine, but the script that is supposed to write to the file won't open it (both scripts and the file are in the same directory). If you want to try it on my server the address is: http://ajt-mine1.no-ip.org.

Code: Select all

<html>
<body>
<?php
        $myFile = "chat.txt";
        $handle = fopen($myFile, "a") or die("Could not open file");
        $name = $_POST["name"];
        $post = $_POST["message"];
        $resultLine = $name . ": " .  $post . "\n";
        fwrite($handle, $resultLine);
        echo "Result Line: " . $resultLine;
        fclose($handle);
        http_redirect("index.php");     
?>
</body>
</html>

Re: PHP Writing to a file

Posted: Fri Nov 11, 2011 5:08 pm
by Falco Girgis
Sounds like write permission issues on that directory on your server...

Re: PHP Writing to a file

Posted: Fri Nov 11, 2011 6:57 pm
by dandymcgee
GyroVorbis wrote:Sounds like write permission issues on that directory on your server...
Good guess. Have you checked on this yet?

Re: PHP Writing to a file

Posted: Fri Nov 11, 2011 10:52 pm
by superLED
Have you checked your php configuration file?

Re: PHP Writing to a file

Posted: Sun Nov 13, 2011 8:01 pm
by ajtgarber
Yeah, I looked in the apache log and it turned out to be a permissions issue, now I'm wondering how I solve it. In my configuration file I have the server admin set as my user @localhost, and the owners of the files should also be owned by my user. Tell me if you want me to post the config file

Re: PHP Writing to a file

Posted: Mon Nov 14, 2011 8:42 am
by Falco Girgis
Ah, shit. I haven't touched PHP or web development since I was 14, and I certainly didn't look at your code. I only called it, because I vividly remembered fighting that same shit in my youth. ;)

Re: PHP Writing to a file

Posted: Mon Nov 14, 2011 11:30 am
by Tobiski
ajtgarber wrote:Yeah, I looked in the apache log and it turned out to be a permissions issue, now I'm wondering how I solve it. In my configuration file I have the server admin set as my user @localhost, and the owners of the files should also be owned by my user. Tell me if you want me to post the config file
try setting chmod to 775 if it isn't already, that should give you the right amount of permissions.

Re: PHP Writing to a file

Posted: Mon Nov 14, 2011 12:32 pm
by ajtgarber
Chmodding the chat.txt? If so, its still telling me that it can't open the file (for writing)

Re: PHP Writing to a file

Posted: Tue Nov 15, 2011 8:03 pm
by Aleios
ajtgarber wrote:Chmodding the chat.txt? If so, its still telling me that it can't open the file (for writing)
Throw everything you can at it. Try chmod to 777, try the mode "a+" after that. Sometimes you just need to figure out whats going wrong via brute force.

Re: PHP Writing to a file

Posted: Thu Nov 17, 2011 1:59 pm
by CC Ricers
Just using "a" is fine here because this is in a piece of code meant for writing only. "a+" is only needed if you plan to both read and write from the same file handle while it's open.

Re: PHP Writing to a file

Posted: Thu Nov 17, 2011 3:00 pm
by ajtgarber
I have gotten it to work with the chmod 777, but I'm wondering how I would solve this long-term so I wouldn't have to continually do this to files that would need to be edited by my PHP scripts.
I think apache is running as root because I took an upload script of the net and tested it with a simple text file, and the file was write protected. I could be completely wrong on this, I'm (sadly)
still not entirely used to UNIX permissions :P

Re: PHP Writing to a file

Posted: Sun Nov 20, 2011 1:48 am
by Aleios
Well, good old wikipedia can help you to learn a chmod:
http://en.wikipedia.org/wiki/Chmod

What's strange is the fact that the script is being run under the "others" category, that is why using 777 worked, as 777 means give permission to all, and 775 means give "others" permission to read and execute. Try 776 or 772.

So for some reason it looks like the server believes itself to be someone else. As i believe most of you know, PHP is a server sided scripting language, therefore the server runs the script and not the user. Basically, this is weird, or i could be reading the chmod wrong myself.