Appending to Text file with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kvigorous
Forum Newbie
Posts: 2
Joined: Sun Sep 05, 2004 1:26 pm

Appending to Text file with php

Post by kvigorous »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi all, 
I'm trying to make a simple database using a text file that gets its values from a form. I want to set up the script so that each item is separated by a comma. Then the data from each new submission from the form gets written to the next line. Here's what I have so far:

Code: Select all

<?php 
$fp = fopen("submission.txt","a"); 
$content = "Band Name:".$_POST["band_name"]." Contact:".$_POST["contact"]." Email:".$_POST["email"]." Home Phone:".$_POST["home_phone"]." Cell Phone:".$_POST["cell_phone"]." Contact:".$_POST["city"]." Contact:".$_POST["state"]." Contact:".$_POST["link"]." Contact:".$_POST["bio"]."\r\n"; 
fwrite($fp,$content); 
fclose($fp); 
?>
I know that this isn't right to separate the fields by commas, but I get this error when I submit from the form:

Code: Select all

Warning: fopen("submission.txt", "a") - Permission denied in /home/httpd/vhosts/arxposure.com/httpdocs/db/submission2.php on line 2 

Warning: fwrite(): supplied argument is not a valid File-Handle resource in /home/httpd/vhosts/arxposure.com/httpdocs/db/submission2.php on line 5 

Warning: fclose(): supplied argument is not a valid File-Handle resource in /home/httpd/vhosts/arxposure.com/httpdocs/db/submission2.php on line 6
By the way, I have set the permissions for the directory for full read/write. Thanks very much for the help, everyone.


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

you need to make sure the file itself has write access for the user that runs the webserver... Usually this is done with chmod o+r /the/file (assuming www-data/nobody is not in the group)

Btw:
I don't see any comma's being output at all in your script?
And each line having redundant data as "Contact: ", "Email: ", etc doesn't seem very usefull either (Store the names of each columns once as the first row in your file fe.. this way even Excell can use them easily :) :)
kvigorous
Forum Newbie
Posts: 2
Joined: Sun Sep 05, 2004 1:26 pm

it works now

Post by kvigorous »

thanks. that did it.
Post Reply