fwrite() command RE: saving information to a txt file
Posted: Tue Jul 30, 2002 10:21 am
I have a bit of code i made up that will allow visitors to edit information on a text file.
this is the following code :-
function insertRow($j)
{
echo "<td><input name=\"dish[$j]\" value=\"Enter Dish\"></td><td><input name=\"price[$j]\" value=\"Enter Price\"></td><td><textarea name=\"description[$j]\" cols=\"47\" rows=\"10\">Enter Description</textarea></td>";
echo "</tr>";
}
if (!$fp)
{
// Report Errors if Any
echo "<h3>Sorry there has been an error with this $fileText[0] and you could not open this file or the file cannot be found.</h3><br>";
#Mail Error to Me!;
mail("kendall\@apex-solutions.com","Error on Save","Error saving to the $fileText[0]","rafters\@6awarnerst.com");
exit;
}else{
global $fp;
$fileContents = file("$DOCUMENT_ROOT/rafters/menus/$file");
fclose($fp); // Close File
$countItems = count($fileContents);
$fileText = explode(".",$file);
// Print Html OutPut
echo "<html>";
require ("top.htm");
require ("instruct.txt");
echo "<form action=\"save_file.php\" method=\"POST \" enctype=\"text/plain\">";
echo "<table border=1>\n";
echo "<tr>";
echo "<th colspan =3>\n";
echo "<H4>EDIT $fileText[0]</h4></th>\n";
echo "<tr>";
echo "<th>Name of Dish</th> <th>Price</th> <th>Description</th>";
echo "</tr>";
for ($i = 0; $i <$countItems; $i++)
{
$value = explode("|",StripSlashes($fileContents[$i]));
echo "<tr>";
echo "<td><input name=\"dish[$i]\" value=\"$value[0]\"></td><td><input name=\"price[$i]\" value=\"$value[1]\"></td><td><textarea name=\"description[$i]\" cols=\"47\" rows=\"10\">$value[2]</textarea></td>";
echo "</tr>";
}
}
if ($functionCall==1)
insertRow($j);
# pass variable along scripts
echo "<tr>";
echo "<td colspan=3><input name = \"submit\" value=\"submit\" type=\"submit\"></td>";
echo "</tr>";
echo "<input name =\"file\" type=\"hidden\" value=\"$file\">";
echo "</table>\n";
echo "</form>\n";
if ($j=="")
echo "<a href=\"menu_editor.php?file=$fileText[0]&j=$i&functionCall=1\">Add Entry</a>";
echo "</body>\n";
echo "</html>\n";
# END
now i have a code that saves the information back to the txt file in a defined format using the fputs command
// Write to the File
while (list($k,$v) = each($dish)) {
# get each variable in format sequence
$d = $v;
$p = $price[$k];
$ds =$description[$k];
$form = "$d|$p|$ds\r";
@ fputs($fp,$form); # write to file command
}
}
// Output to HTML Page
echo "<html>\n";
require ("top.htm");
echo "<p>Menu Saved successfully. The following is what was saved to the $fileText[0]:-</p>";
# display to Html Page
require("display_menu.php");
# END
the problem is that it keeps saving an extra \n and \r carriage return
i want to know how i can eliminate the problem. also how does the fwrite command and fputs command really work
this is the following code :-
function insertRow($j)
{
echo "<td><input name=\"dish[$j]\" value=\"Enter Dish\"></td><td><input name=\"price[$j]\" value=\"Enter Price\"></td><td><textarea name=\"description[$j]\" cols=\"47\" rows=\"10\">Enter Description</textarea></td>";
echo "</tr>";
}
if (!$fp)
{
// Report Errors if Any
echo "<h3>Sorry there has been an error with this $fileText[0] and you could not open this file or the file cannot be found.</h3><br>";
#Mail Error to Me!;
mail("kendall\@apex-solutions.com","Error on Save","Error saving to the $fileText[0]","rafters\@6awarnerst.com");
exit;
}else{
global $fp;
$fileContents = file("$DOCUMENT_ROOT/rafters/menus/$file");
fclose($fp); // Close File
$countItems = count($fileContents);
$fileText = explode(".",$file);
// Print Html OutPut
echo "<html>";
require ("top.htm");
require ("instruct.txt");
echo "<form action=\"save_file.php\" method=\"POST \" enctype=\"text/plain\">";
echo "<table border=1>\n";
echo "<tr>";
echo "<th colspan =3>\n";
echo "<H4>EDIT $fileText[0]</h4></th>\n";
echo "<tr>";
echo "<th>Name of Dish</th> <th>Price</th> <th>Description</th>";
echo "</tr>";
for ($i = 0; $i <$countItems; $i++)
{
$value = explode("|",StripSlashes($fileContents[$i]));
echo "<tr>";
echo "<td><input name=\"dish[$i]\" value=\"$value[0]\"></td><td><input name=\"price[$i]\" value=\"$value[1]\"></td><td><textarea name=\"description[$i]\" cols=\"47\" rows=\"10\">$value[2]</textarea></td>";
echo "</tr>";
}
}
if ($functionCall==1)
insertRow($j);
# pass variable along scripts
echo "<tr>";
echo "<td colspan=3><input name = \"submit\" value=\"submit\" type=\"submit\"></td>";
echo "</tr>";
echo "<input name =\"file\" type=\"hidden\" value=\"$file\">";
echo "</table>\n";
echo "</form>\n";
if ($j=="")
echo "<a href=\"menu_editor.php?file=$fileText[0]&j=$i&functionCall=1\">Add Entry</a>";
echo "</body>\n";
echo "</html>\n";
# END
now i have a code that saves the information back to the txt file in a defined format using the fputs command
// Write to the File
while (list($k,$v) = each($dish)) {
# get each variable in format sequence
$d = $v;
$p = $price[$k];
$ds =$description[$k];
$form = "$d|$p|$ds\r";
@ fputs($fp,$form); # write to file command
}
}
// Output to HTML Page
echo "<html>\n";
require ("top.htm");
echo "<p>Menu Saved successfully. The following is what was saved to the $fileText[0]:-</p>";
# display to Html Page
require("display_menu.php");
# END
the problem is that it keeps saving an extra \n and \r carriage return
i want to know how i can eliminate the problem. also how does the fwrite command and fputs command really work