PHP Writing to a file

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

PHP Writing to a file

Post 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>
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: PHP Writing to a file

Post by Falco Girgis »

Sounds like write permission issues on that directory on your server...
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: PHP Writing to a file

Post by dandymcgee »

GyroVorbis wrote:Sounds like write permission issues on that directory on your server...
Good guess. Have you checked on this yet?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: PHP Writing to a file

Post by superLED »

Have you checked your php configuration file?
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: PHP Writing to a file

Post 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
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: PHP Writing to a file

Post 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. ;)
User avatar
Tobiski
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Sun Sep 06, 2009 4:59 am
Current Project: 2D Maze/Puzzle
Favorite Gaming Platforms: PC (Windows&Linux)
Programming Language of Choice: C/C++
Location: Oulu, Finland
Contact:

Re: PHP Writing to a file

Post 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.
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: PHP Writing to a file

Post by ajtgarber »

Chmodding the chat.txt? If so, its still telling me that it can't open the file (for writing)
Aleios
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Mon Feb 21, 2011 2:55 am
Current Project: Aleios Engine
Favorite Gaming Platforms: PC, Dreamcast
Programming Language of Choice: C++
Location: Melbourne, Australia

Re: PHP Writing to a file

Post 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.
Image
CC Ricers
Chaos Rift Regular
Chaos Rift Regular
Posts: 120
Joined: Sat Jan 24, 2009 1:36 am
Location: Chicago, IL

Re: PHP Writing to a file

Post 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.
ajtgarber
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Wed Jun 10, 2009 8:56 am

Re: PHP Writing to a file

Post 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
Aleios
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Mon Feb 21, 2011 2:55 am
Current Project: Aleios Engine
Favorite Gaming Platforms: PC, Dreamcast
Programming Language of Choice: C++
Location: Melbourne, Australia

Re: PHP Writing to a file

Post 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.
Image
Post Reply