Jump to content

Having Some Problems with reading and writing in files


vivalaraza

Recommended Posts

Hey after trying to find the error for ages i give up for now, well if you can find it and show me i will be greatly appreciated:

 

$txt = "random/new.txt";

 

$i= 1;

 

$line[0] = "somedata...."; // this gets the info from the form and adds the line to the first value of the array

 

$fp = fopen($txt,"rb"); // need to use rb cause windows

 

////then the file is read into the array starting from $line[1]

 

while(!feof($fp)) {

   

$line[$i] = fgets($fp);

 

if (empty($line[$i])) {

unset($line[$i]);

$i = $i - 1;

}

       

$i++;

}

 

fclose($fp);

 

/// now the whole array is read back into the file

 

$i = 0;

 

$total = count($line);

 

$fp = fopen($txt,"wb"); //need to use rb cause windows

 

while ($i < $total) {

     

if ($i <> $total - 1) {

  fwrite($fp,"$line[$i]\r\n");  //need to use \r\n cause windows

}

else {

fwrite($fp,"$line[$i]");   

}

 

$i++;

 

}

 

fclose($fp);

 

}

 

I want to format it so i can make the first line in the text file be the contents of the form that was just processed,

 

after clicking the button the first time to process the form, it was fine,the 2nd time to process the form its fine,

 

the 3rd time i clicked the button the text file looks something like this:

 

somedata

somedata

                      // and that annoying irrating space in the text file appears

somedata

 

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.