...

View Full Version : Help with writing to a file



Rappa
01-17-2008, 04:42 AM
so i have a form with a name and email field, and the action points to a php file with this code:


<?php
$name = $_POST["name"];

$file = "012608.txt";

$values = "$name\n";

$fp = fopen($file, "w") or die("Couldn't open $file for writing!");
$numBytes = fwrite($fp, $values) or die("Couldn't write values to file!");

fclose($fp);
echo "You have successfully signed up for this class.";

?>


Now when i hit submit it gives the echo with no errors, but when i open the text file in the browser, its blank, any ideas?

Rappa
01-17-2008, 05:15 AM
lol ok well fixed that problem, forgot the method="post" in the form.

But new problem, it works, but when i do it again, it over writes, how can i have it write names one after another without overwriting the previous?

oesxyl
01-17-2008, 05:35 AM
lol ok well fixed that problem, forgot the method="post" in the form.

But new problem, it works, but when i do it again, it over writes, how can i have it write names one after another without overwriting the previous?

:), use "w+" instead of "w" in fopen, :)

best regards

Mwnciau
01-17-2008, 05:38 AM
w and w+ both 'truncate the file to zero length', you need to use a or a+

oesxyl
01-17-2008, 05:48 AM
w and w+ both 'truncate the file to zero length', you need to use a or a+

you are right, + is for reading, sorry for the wrong answer.

best regards

Rappa
01-17-2008, 06:01 AM
Thanks guys it worked. You rox

Rappa
01-17-2008, 06:01 AM
just out of curiosity, whats the difference between a and a+

Mwnciau
01-17-2008, 06:13 AM
a is just like w, its for writing only, both a+ and w+ can read as well as write, it's explained on the manual page for fopen (http://uk3.php.net/manual/en/function.fopen.php)

StupidRalph
01-17-2008, 06:13 AM
These are all the different writing modes.
http://www.php.net/manual/en/function.fopen.php

mode Description
'r' Open for reading only; place the file pointer at the beginning of the file.
'r+' Open for reading and writing; place the file pointer at the beginning of the file.
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

Rappa
01-17-2008, 06:16 AM
Thank you kindly!

oesxyl
01-17-2008, 06:18 AM
just out of curiosity, whats the difference between a and a+

a+ open add reading mode to a.
In fact + means both mode, writing and reading, thats why could be w+, r+, a+.

EDIT: Sorry, for repeating the same thing, but when I start edit this, was no answer, :-)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum