Jump to content

a file read/write problem...please help!


phpaddict

Recommended Posts

im using PHP4.3 on WIN XP.  (installed with the windows installer)

i made this form handler page. 

its supposed to get information from a form through $_POST, and then append it to a file, and then read and display the updated information.

The writing part is okay,

But the problem is this:  it reads the previous information, not the latest updated one. 

Since it writes BEFORE it reads, the informaiton should be updated with the new post, right?

But it only displays the old contents of the file, without the latest update (the one last appended).

what am i doing wrong?

[code]

<html>
<body>


<?php



global $name,$email,$info;

$name = "";
$email = "";
$quote = "";

$name = $_POST['name'];
$email = $_POST['email'];
$info= $_POST[info];


WriteInfo($name,$email,$info);
ReadInfo();


// THIS FUNCTION WRITES THE INFO TO THE FILE
function WriteInfo($n,$e,$i) {



$filename = "d:\scripts\one.txt";
if(file_exists($filename))
{
$fs = fopen($filename, "a");
fputs($fs,$n."*".$e."*".$i."**");
fclose($fs);
}
else {
print "Error: file does not exist at d:\scripts\one.txt";
}

}






// THIS FUNCTION READS THE INFO FROM THE FILE
function ReadInfo() {


$filename = "d:\scripts\one.txt";

touch($filename);

if(file_exists($filename)) {
$fs = fopen($filename,"r");
$output = fgets($fs,filesize($filename));

print "The Info is:";
print "<br>$output";
}
else
{
print "file does not exist at d:\scripts\one.txt";
}

}

?>

</body>

</html>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/32956-a-file-readwrite-problemplease-help/
Share on other sites

I made a test version of your problem script:
[code]<?php

$name = "";
$email = "";
$quote = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$quote = $_POST['quote'];
WriteInfo($name,$email,$quote);
ReadInfo();
}
else {
echo '<form method="post">';
echo 'Name: <input name="name" type="text"><br>';
echo 'Email: <input name="email" type="text"><br>';
echo 'Quote: <input name="quote" type="text"><br>';
echo '<input type="submit" name="submit"></form>';
}

// THIS FUNCTION WRITES THE INFO TO THE FILE
function WriteInfo($n,$e,$q) {
$filename = "scripts/one.txt";
if(file_exists($filename))
{
$fs = fopen($filename, "a");
fwrite($fs,$n."*".$e."*".$q."**\r\n");
fclose($fs);
} else {
print "Error: file does not exist at d:\scripts\one.txt";
}
}

// THIS FUNCTION READS THE INFO FROM THE FILE
function ReadInfo() {
$filename = "scripts/one.txt";
touch($filename);
if(file_exists($filename)) {
$output = file($filename);
print "The Info is:<br>";
foreach ($output as $line)
echo nl2br($line);
} else {
print "file does not exist at scripts/one.txt";
}
}
?>[/code]

I changed the fputs/fgets to fwrite and the file() functions.

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.